How To The Write `datePart( datepart, datetime )` Function In ColdFusion?
Description:-
This is used to get a specific part from a datetime object.
datePart( datepart_argument, datetime_object );
This function returns a part of a date, as an integer value.
Member Function Syntax:-
datetime_object.datePart( datepart_argument );
Attributes:-
datepart:-
This is a required argument and it is a string
field, the accepted values are in the below list.
datepart_value | description |
yyyy | It will return the year. |
q | It will return the quarter of the year. |
m | It will return the month. |
y | It will return the day number of the year. |
d | It will return the day number of the month. |
w | It will return the weekday number of 7 weekdays. |
ww | It will return the week of the year. |
h | It will return the hour. |
n | It will return the minute. |
s | It will return the second. |
l | It will return the millisecond. |
date:-
This is a required argument, A date/time object in the range 100 AD – 9999 AD.
Example:-
var dateObject = "2025-11-21 04:35:45";
writeoutput( "The date is: #dateObject#.<br>" );
writeoutput( "The year: #datePart( "yyyy", dateObject )#.<br>" );
writeoutput( "The quarter: #datePart( "q", dateObject )#.<br>" );
writeoutput( "The month: #datePart( "m", dateObject )#.<br>" );
writeoutput( "The day of the year: #datePart( "y", dateObject )#.<br>" );
writeoutput( "The day of the month: #datePart( "d", dateObject )#.<br>" );
writeoutput( "The weekday of 7 weekdays: #datePart( "w", dateObject )#.<br>" );
writeoutput( "The week of the year: #datePart( "ww", dateObject )#.<br>" );
writeoutput( "The hour: #datePart( "h", dateObject )#.<br>" );
writeoutput( "The minute: #datePart( "n", dateObject )#.<br>" );
writeoutput( "The second: #datePart( "s", dateObject )#.<br>" );
writeoutput( "The millisecond: #datePart( "l", dateObject )#." );
Result:-
The date is: 2025-11-21 04:35:45.
The year: 2025.
The quarter: 4.
The month: 11.
The day of the year: 325.
The day of the month: 21.
The weekday of 7 weekdays: 6.
The week of the year: 47.
The hour: 4.
The minute: 35.
The second: 45.
The millisecond: 0.