Description

Extracts a part from a date value.

Returns

Part of a date, as an integer.

Category

Date and time functions

Function syntax

DatePart(datepart, date)

See also

DateAddDateConvert

History

ColdFusion MX 6.1: Added the datepart character L or l to represent milliseconds.

Parameters

Parameter

Description

datepart

String:

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

date

Date/time object, in the range 100 AD-9999 AD.

Usage

When passing a date/time object as a string, enclose it in quotation marks. Otherwise, it is interpreted as a numeric representation of a date/time object.

Example

<cfscript>
    Date1 = "{ts '2018-11-15 12:13:50'}";
    DatePart= DatePart("yyyy",Date1)
    DatePart1= DatePart("m",Date1)
    DatePart2= DatePart("d",Date1)
    DatePart3= DatePart("h",Date1)
    DatePart4= DatePart("n",Date1)
    DatePart5= DatePart("s",Date1)
    writeOutput("year is : " & DatePart & "<br/>")
    writeOutput("month is : " & DatePart1 & "<br/>")
    writeOutput("day is : " & DatePart2 & "<br/>")
    writeOutput("hour is : " & DatePart3 & "<br/>")
    writeOutput("minute is : " & DatePart4 & "<br/>")
    writeOutput("second is : " & DatePart5 & "<br/>")
</cfscript>

Output

year is : 2018
month is : 11
day is : 15
hour is : 12
minute is : 13
second is : 50

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy