DatePart

Return a specified part of a given date.

Syntax 
      DatePart (interval,date [, FirstDayofWeek [, FirstWeekofYear]])

Key
    date       The date to analyse

FirstDayofWeek A constant defining the first day of the week:
               vbUseSystem (0), vbSunday (1=default),vbMonday(2), 
               vbTuesday(3), vbWednesday(4), vbThursday(5),
               vbFriday(6), vbSaturday(7)

FirstWeekofYear A constant defining the first week of the year:
               vbUseSystem(0), vbFirstJan1(1),vbFirstFourDays(2),
               vbFirstFullWeek(3) 

    interval   The date/Time interval to express in the result:

                 yyyy  Year
                  q    Quarter
                  m    Month
                  y    Day of year
                  d    Day
                  w    Weekday
                  h    Hour
                  n    Minute
                  s    Second

Examples

dtm1="18-Feb-10"
WScript.Echo dtm1
intMonthsinDate=DatePart("m", dtm1)
WScript.Echo intMonthsinDate

To produce a correct ISO weeknumber requires a function:

Function ISOWeekNum(dtmDate)
  ' Returns a WeekNumber from a date
  Dim NearThurs
  NearThurs = ((dtmDate+5) \ 7) * 7 - 2
  ISOWeekNum = ((NearThurs - DateSerial(Year(NearThurs), 1, 1)) \ 7) + 1
End function

' Example
wscript.echo "ISOWeekNumber: " & ISOWeekNum(cdate("2017-12-25"))

“Christmas, children, is not a date. It is a state of mind” ~ Mary Ellen Chase

Related VBScript commands

Date - The current system date.
Day - Return the day component of a date.
DateAdd - Add a time interval to a Date.
DateDiff - Return the time interval between two dates.
DateSerial - Return a Date from a numeric Year, Month and Day.
Month - Return the month component of a date.
Year - Return the year component of a date.
Q200299 - Format or DatePart Functions return wrong Week number for last Monday in year.
Standard date and time notation - YYYY-MM-DD
Equivalent PowerShell cmdlet: Get-Date


 
Copyright © 1999-2024 SS64.com
Some rights reserved