DateTimeFormat()
Formats a date object to a given string output.
DateTimeFormat( datetime=any, mask=string, timezone=timezone );
Returns: String
Argument | Description | Default |
---|---|---|
datetime
any,
required
|
date object; for example, now() Alias: date |
|
mask
string,
optional
|
Date time formatting mask (case sensitive):
The following masks can be used to format the full date and time and may not be combined with other masks:
|
dd-MMM-yyyy HH:nn:ss |
timezone
timezone,
optional
|
A datetime object is independent of a specific timezone; it is only an offset in milliseconds from The timezone only comes into play when you need specific information like hours in a day, minutes in a 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(). You can find a list of all available timezones in the Lucee administrator (Settings/Regional). Some examples of valid timezones include:
|
Examples
//format the date parts as like as dateformat()
//formant the date & time by user defined date
dateandtime = createDateTime(2015,02,04,11,22,33);
writeDump(dateTimeFormat(dateandtime, "GG-dddd, dd/mmm/yyyy,hh:nn:ss tt,zzzz"));
<span class="c">//formant the date & time by server time</span>
<span class="nv">dateandtime</span> <span class="o">=</span> <span class="nf">createDateTime</span><span class="p">(</span><span class="nf">year</span><span class="p">(</span><span class="nf">now</span><span class="p">()),</span><span class="nf">month</span><span class="p">(</span><span class="nf">now</span><span class="p">()),</span><span class="nf">day</span><span class="p">(</span><span class="nf">now</span><span class="p">()),</span><span class="nf">hour</span><span class="p">(</span><span class="nf">now</span><span class="p">()),</span><span class="nf">minute</span><span class="p">(</span><span class="nf">now</span><span class="p">()),</span><span class="nf">second</span><span class="p">(</span><span class="nf">now</span><span class="p">()));</span>
<span class="nf">writeDump</span><span class="p">(</span><span class="nf">dateTimeFormat</span><span class="p">(</span><span class="nv">dateandtime</span><span class="p">,</span> <span class="s2">"dddd, dd/mmm/yyyy,hh:nn:ss tt,zzzz"</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateTimeFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s2">"mm/dd/yyyy"</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateTimeFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s2">"MM/DD/YYYY"</span><span class="p">));</span>
<span class="nf">writeDump</span><span class="p">(</span><span class="nf">dateTimeFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s2">"EEEE-dd,mm,yyyy"</span><span class="p">));</span>
<span class="c">//Member function</span>
<span class="nv">dt</span><span class="o">=</span><span class="nf">createDate</span><span class="p">(</span><span class="m">2015</span><span class="p">,</span><span class="m">04</span><span class="p">,</span><span class="m">14</span><span class="p">);</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dt.dateTimeFormat</span><span class="p">(</span><span class="s2">"full"</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dt.dateTimeFormat</span><span class="p">(</span><span class="s2">"long"</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dt.dateTimeFormat</span><span class="p">(</span><span class="s2">"medium"</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dt.dateTimeFormat</span><span class="p">(</span><span class="s2">"short"</span><span class="p">));</span>
<span class="nf">writeDump</span><span class="p">(</span><span class="nf">dt.dateTimeFormat</span><span class="p">(</span><span class="s2">"GG-dddd, dd/mmm/yyyy,hh:nn:ss tt,zzzz"</span><span class="p">));</span>
<span class="nf">writeDump</span><span class="p">(</span><span class="nf">now</span><span class="p">().</span><span class="nf">dateTimeFormat</span><span class="p">(</span><span class="s2">"ISO8601"</span><span class="p">));</span>
See also
- Date and time
- Strings
- Formatting
- GetHttpTimeString()
- LSDateTimeFormat()
- datetime.dateTimeFormat()
- Search Issue Tracker
- Search Lucee Test Cases (good for further, detailed examples)