|
Extracting parts of the date out with DatePart()
DatePart(PartOfDate, Date[, FirstDayOfWeek, FirstDayOfYear])
- returns the request part of the date
- optional parameters are setting the first day of the week (defaults to Sunday), and setting the first week of the year (defaults to Jan 1).
FirstDayOfWeek optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Sunday (the default value if this parameter is not supplied)
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thursday
6 - Friday
7 - Saturday
FirstDayOfYear optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Start with the week that has Jan 1 in it (the default value if this parameter is not supplied)
2 - Start with the first week that contains at least four days in the new year.
3 - Start with the first week that is a full week in the new year.
- I recommend just leaving the two optional parameters out unless they specifically apply to what you are currently trying to do.
PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
An example of what each of those values will output is:
var1 = Now()
Response.Write("var1 = " & var1 & "<br>")
Response.Write("var1 part yyyy = " & DatePart("yyyy", var1) & "<br>")
Response.Write("var1 part m = " & DatePart("m", var1) & "<br>")
Response.Write("var1 part q = " & DatePart("q", var1) & "<br>")
Response.Write("var1 part y = " & DatePart("y", var1) & "<br>")
Response.Write("var1 part d = " & DatePart("d", var1) & "<br>")
Response.Write("var1 part w = " & DatePart("w", var1) & "<br>")
Response.Write("var1 part ww = " & DatePart("ww", var1) & "<br>")
Response.Write("var1 part h = " & DatePart("h", var1) & "<br>")
Response.Write("var1 part n = " & DatePart("n", var1) & "<br>")
Response.Write("var1 part s = " & DatePart("s", var1) & "<br>")
Results:
var1 = 9/1/99 1:18:15 PM
var1 part yyyy = 1999
var1 part m = 9
var1 part q = 3
var1 part y = 244
var1 part d = 1
var1 part w = 4
var1 part ww = 36
var1 part h = 13
var1 part n = 18
var1 part s = 15
Adding & Subtracting Dates & Times
VBScript provides developers with a wonderful function called DateAdd(). With this function you can add or subtract from a date or time.
DateAdd(PartOfDate, AmountToChange, Date)
- returns a date that has been changed the specified amount
- AmountToChange is the number to which you want the date to change (1, -4, 10, etc).
- Date is a valid date
- PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
An example of what each of those values will output is:
var1 = Now()
Response.Write("var1 = " & var1 & "<br>")
Response.Write("var1 plus 10 days = " & DateAdd("d", 10, var1) & "<br>")
Response.Write("var1 minus 10 days = " & DateAdd("d", -10, var1) & "<br>")
Response.Write("var1 plus 3 months = " & DateAdd("m", 3, var1) & "<br>")
Response.Write("var1 minus 3 months = " & DateAdd("m", -3, var1) & "<br>")
Response.Write("var1 plus 2 hours = " & DateAdd("h", 2, var1) & "<br>")
Response.Write("var1 minus 2 hours = " & DateAdd("h", -2, var1) & "<br>")
Results:
var1 = 9/1/99 1:41:04 PM
var1 plus 10 days = 9/11/99 1:41:04 PM
var1 minus 10 days = 8/22/99 1:41:04 PM
var1 plus 3 months = 12/1/99 1:41:04 PM
var1 minus 3 months = 6/1/99 1:41:04 PM
var1 plus 2 hours = 9/1/99 3:41:04 PM
var1 minus 2 hours = 9/1/99 11:41:04 AM
Find the amount of time between dates with DateDiff()
Find out how much time occurs between two dates is easy with DateDiff(). This function is useful if you need to find the amount of time between one date and another, or if you need to find out how much time is left until the end of the year.
DateDiff(PartOfDate, Date1, Date2[, FirstDayOfWeek, FirstDayOfYear])
- returns the amount of time in between the two dates in the form of PartOfDate, therefore if you set PartOfDate equal to year, the function will tell you how many years are in between Date1 and Date2 even if you are comparing Dec 31 and Jan 1. If you set PartOfDate equal to day then the function will return to you the number of days between Date1 and Date2.
- if Date1 occurs after Date2 the function will return to you a negative number.
- optional parameters are setting the first day of the week (defaults to Sunday), and setting the first week of the year (defaults to Jan 1).
FirstDayOfWeek optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Sunday (the default value if this parameter is not supplied)
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thursday
6 - Friday
7 - Saturday
FirstDayOfYear optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Start with the week that has Jan 1 in it (the default value if this parameter is not supplied)
2 - Start with the first week that contains at least four days in the new year.
3 - Start with the first week that is a full week in the new year.
- I recommend just leaving the two optional parameters out unless they specifically apply to what you are currently trying to do.
PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
Example of use:
var1 = "04/04/1999"<br> var2 = "01/11/2000"<br>
Response.Write("var1 = " & var1 & "<br>")
Response.Write("var2 = " & var2 & "<br>")
Response.Write("var1 to var2 is " & DateDiff("d", var1, var2) & " days <br>")
Response.Write("var1 to var2 is " & DateDiff("m", var1, var2) & " months <br>")
Response.Write("var1 to var2 is " & DateDiff("yyyy", var1, var2) & " year(s) <br>")
Results:
var1 = 04/04/1999
var2 = 01/11/2000
var1 to var2 is 282 days
var1 to var2 is 9 months
var1 to var2 is 1 year(s)
Getting the day, month, year, weekday, and weekday name
There are five functions that you can call, pass a valid date, and they will return to you the requested part of the date. Below I will detail each of them and show an example of their use:
Day(ValidDate)
- returns the day of the ValidDate, a number between 1 and 31
Month(ValidDate)
- returns the month of the ValidDate, a number between 1 and 12
Year(ValidDate)
- returns the year of the ValidDate, a number like 1997
Weekday(ValidDate[, FirstDayOfWeek])
- returns the weekday of the ValidDate, a number 1 - 7
- what the function returns depends on what day is the first day of the week, you can set this, or use the default - Sunday
- to set the FirstDayOfWeek parameter use 1-7, where 1 is Sunday
WeekDayName(DayOfWeek, Abbreviate, FirstDayOfWeek)
- returns the name of the day of the week that you specify.
- DayOfWeek is a number representing the day of the week you want the name for (1-7)
- Abbreviate is a boolean value to tell the function whether or not you want the name that comes back to be abbreviated or not (true or false)
- to set the FirstDayOfWeek parameter use 1-7, where 1 is Sunday
An example of the above functions in use is:
var1 = "04/04/1999"<br>
Response.Write("var1 = " & var1 & "<br>")
Response.Write("For var1 the day is " & Day(var1) & "<br>")
Response.Write("For var1 the month is " & Month(var1) & "<br>")
Response.Write("For var1 the year is " & Year(var1) & "<br>")
Response.Write("For var1 the weekday is " & WeekDay(var1, 1) & "<br>")
Response.Write("For var1 the weekdayname is " & WeekDayName(WeekDay(var1), false, 1) & "<br>")
Results:
var1 = 04/04/1999
For var1 the day is 4
For var1 the month is 4
For var1 the year is 1999
For var1 the weekday is 1
For var1 the weekdayname is Sunday
|