Page 1 of 1

VB6 - Date And Time Functions As its clear from topic's title Rate Topic: -----

#1 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Post icon  Posted 27 January 2009 - 11:14 AM

Date and time functions allows us to perform various operations related to date and time in VB6.

1) Now Function

Explanation : It Returns both date and time stored in computer's clock.

Syntax:


Now()




Example :


Print Now()

'Output is 1/27/2009 11:14:58 PM




2) Date and Date$ Function

Explanation : Both functions returns the system date.

NOTE : Date function reuturns system date in VARIANT datatype where as Date$ function returns it in STRING datatype

Syntax:


Date()

Date$()




Example :


Print Date()

'Output is 1/27/2009

Print Date$()

'Output is 01-27-2009




3) Time and Time$ Function

Explanation : Returns System time.

NOTE : Time() function returns VARIANT datatype
Time$() function returns STRING datatype

Syntax:


Time()

Time$()




Example:


Print time()

'Output is 11:20:04 PM

Print time$()

'Output is 23:20:04




4) DatePart FUnction:

Explanation :

It is used to extract various parts of the date/time.

Syntax :


DatePart(interval, validDate)

' interval : String expression that is the part you want to return.
' validDate : Variant(Date) that you want to evaluate.

'Interval's:

"yyyy" Year 

"q" Quarter 

"m" Month 

"y" Day of year 

"d" Day 

"w" Weekday 

"ww" Week 

"h" Hour 

"n" Minute 

"s" Second 





Example :


Print "Current Month is " & DatePart("m", Now)

'Output is 1




5) Day, Month, year Functions

Explanation :

Returns their Day, month and year in numeric form

Syntax :


Day(DateArgument)
Month(DateArgument)
Year(DateArgument)




Example :


Print Day(Now)

'Output is 27

Print Month(Now)

'Output is 1

Print Year(Now)

'Output is 2009




6) Hour, Minute and Second Functions

Explanation : Returns hour, minute, and seconds in numeric form

Syntax:


Hour(DateArgument)
Minute(DateArgument)
Second(DateArgument)




Example:


Print Hour(Now)

'Output is 23

Print Minute(Now)

'Output is 32

Print Second(Now)

'Output is 56

'Current Time is 11:32:56 PM




7) TImer function

Explanation: Returns no. of seconds passed since midnight

Syntax:


Timer()




Example:


Print Timer()

'Output is 84878.33, i.e, this much seconds have passed since midnight.

'current time is 11:35 PM with some seconds.




8) DateAdd Function

The DateAdd function enables you to add a specified time interval to a date.



Syntax:


DateAdd(interval, number, date)





The DateAdd function syntax has these parts:


Part
Description

interval
Required. String expression that is the interval of time that you want to add.



The string expression can be any of the following:



Expression Description

"yyyy" Year

"q" Quarter

"m" Month

"y" Day of year

"d" Day

"w" Weekday

"ww" Week

"h" Hour

"n" Minute

"s" Second



Note: To add days to date, you can use either "d", "y", or "w". Be advised that the term "weekday" ("w") has nothing to do with skipping weekend days; i.e., it is NOT equivalent to "business days". Applications that would require skipping weekend days (like Saturday and/or Sunday) would have to be custom coded.



number
Required. Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).



date
Required. Date value that you want to evaluate.

9) DateDiff Function

Syntax:


DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])





The DateDiff function syntax has these parts:


Part
Description

interval
Required. String expression that is the interval of time you use to calculate the difference between date1 and date2..



The string expression can be any of the following:


Expression: Same used in DateAdd.

date1, date2
Required; Date. The two dates you want to use in the calculation



firstdayofweek
Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.



firstweekofyear
Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.

Few Important Points:

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.



To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.



If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.



The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.



If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.



When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

=======================END======================

That is all about Date and time functions!

Is This A Good Question/Topic? 1
  • +

Replies To: VB6 - Date And Time Functions

#2 haidar976  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-August 11

Posted 05 September 2011 - 07:17 AM

nice work
1/27 is my birthday :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1