datePart

Extracts a part from a datetime value as a numeric.

datePart(datepart, date [,timezone]) → returns numeric

Member Function Syntax

date.datePart(datepart)

Argument Reference

datepart string
Required

yyyy: Year
q: Quarter
m: Month
y: Day of year
d: Day
w: Weekday
ww: Week
h: Hour
n: Minute
s: Second
l: Millisecond
Values:
  • yyyy
  • q
  • m
  • y
  • d
  • w
  • ww
  • h
  • n
  • s
  • l

date date
Required

Datetime object (100AD-9999AD).

timezone string
Default: Timezone Specified in Lucee Administrator

This is only available in Lucee.
A datetime object is independent of a specific timezone; it is only an offset in milliseconds from 1970-1-1 00.00:00 UTC.
The timezone only comes into play when you need specific information like hours in a day, minutes in an hour or which day it is, as these calculations depend on the timezone.
A timezone must be specified in order to translate the date object to something else. If you do not provide the timezone in the function call, it will default to the timezone specified in the Lucee Administrator (Settings/Regional), or the timezone specified for the current request using the function setTimezone().

Compatibility

ColdFusion:

Member function is available is CF11+.

Lucee:

Member function is available is Lucee4.5+. The syntax for this member function is date.part(datepart).

Examples
Sample code invoking the datePart function

This example shows information available from datePart

<cfset todayDate = now()>
<h3>datePart Example</h3>
<p>Today's date is <cfoutput>#todayDate#</cfoutput>.
<p>Using datePart, we extract an integer representing the dateparts from that value <cfoutput>
<ul>
<li>year: #datePart("yyyy", todayDate)#</li>
<li>quarter: #datePart("q", todayDate)#</li>
<li>month: #datePart("m", todayDate)#</li>
<li>day of year: #datePart("y", todayDate)#</li>
<li>day: #datePart("d", todayDate)#</li>
<li>weekday: #datePart("w", todayDate)#</li>
<li>week: #datePart("ww", todayDate)#</li>
<li>hour: #datePart("h", todayDate)#</li>
<li>minute: #datePart("n", todayDate)#</li>
<li>second: #datePart("s", todayDate)#</li>
</ul>
</cfoutput>

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub