DatePart Function

Purpose

Returns an integer containing the specified part of a given date.

Syntax

DatePart(interval, date)

interval: sexp

date: date exp

Description

You can use the DatePart function to evaluate a date and return a specific interval of time. For example, you might use DatePart to calculate the day of the week or the current hour.

The interval argument of time you want to return can have the following values:

"yyyy" Year
"q" Quarter
"m" Month
"y" Day of year
"d" Day
"w" Weekday (1 = Sunday, … , 7 = Saturday)
"ww" Week
"h" Hour
"n" Minute
"s" Second

Example

Debug.Show

Trace DatePart("d", #03/29/1997 23:44:45#)

// Prints 29

Trace DatePart("m", #03/29/1997 23:44:45#)

// Prints 3

Trace DatePart("yyyy", #03/29/1997 23:44:45#)

// Prints 1997

Trace DatePart("y", #03/29/1997 23:44:45#)

// Prints 260

Trace DatePart("q", #03/29/1997 23:44:45#)

// Prints 1

Trace DatePart("w", #03/29/1997 23:44:45#)

// Prints 7

Trace DatePart("ww", #03/29/1997 23:44:45#)

// Prints 13

Trace DatePart("h", #03/29/1997 23:44:45#)

// Prints 23

Trace DatePart("n", #03/29/1997 23:44:45#)

// Prints 44

Trace DatePart("s", #03/29/1997 23:44:45#)

// Prints 45

// or

Trace DatePart("h", 3.5)

// Prints 12

Trace DatePart("yyyy", 3.5 + 1500)

// Prints 1904

// or

Debug

Dim d As Date = 36525.9999

Trace d

Trace DatePart("yyyy", d) // 1999

Trace DatePart("m", d) // 12

Trace DatePart("d", d) // 31

Trace DatePart("h", d) // 23

Trace DatePart("n", d) // 59

Trace DatePart("s", d) // 51

// but

Trace DatePart("yyyy", Time) // 1899 (starting point)

Remarks

You can indicate a time in hours, minutes and seconds, separated by “ : “; or only with four numbers for hours and minutes (with or without the using of AM or PM). Using only 4 characters forces the GFABASIC 32 editor to automatically add “:00“ for the seconds. When using AM or PM the editor automatically converts to 24 hour mode. For instance, "#2:24#" will automatically convert to #14:24:00#, and (#2:24AM#) => (#02:24:00#).

See Also

CDate(), Date, Date$, DateAdd(), DateDiff(), DatePart(), DateSerial(), DateTime$(), DateToDmy, DateToDmyHms, DateValue(), Day(), DayNo(), DmyHmsToDate(), DmyToDate(), HmsToTime(), Hour(), IsDate(), Minute(), Month(), Now, Now$(), TimeSerial(), TimeToHms, TimeValue(), Second(), Week(), WeekDay(), Year()

{Created by Sjouke Hamstra; Last updated: 28/09/2014 by James Gaite}