DateFormat()

Formats a date object to a given string output.

DateFormat( date=any, mask=string, timezone=timezone );

Returns: String

Argument Description Default
date
any, required

date object; for example, now()

mask
string, optional

Date formatting mask:

  • d: Day of the month as digits; no leading zero for single-digit days.
  • dd: Day of the month as digits; leading zero for single-digit days.
  • ddd: Day of the week as a three-letter abbreviation.
  • dddd: Day of the week as its full name.
  • m: Month as digits; no leading zero for single-digit months.
  • mm: Month as digits; leading zero for single-digit months.
  • mmm: Month as a three-letter abbreviation.
  • mmmm: Month as its full name.
  • yy: Year as last two digits; leading zero for years less than 10.
  • yyyy: Year represented by four digits.
  • gg: Period/era string. Ignored. Reserved.

The following masks are options to format the full date and cannot be combined with other masks:

  • short: equivalent to m/d/y
  • medium: equivalent to mmm d, yyyy
  • long: equivalent to mmmm d, yyyy
  • full: equivalent to dddd, mmmm d, yyyy

dd-mmm-yy

timezone
timezone, optional

A datetime object is independent of a specific timezone; it is only an offset in milliseconds from 1970-1-1 00.00:00 UTC (Coordinated Universal Time).

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:

  • AGT (for time in Argentina)
  • Europe/Zurich (for time in Zurich/Switzerland)
  • HST (Hawaiian Standard Time in the USA)
  • JVM (JVM / Server Default Timezone)

Examples

// the below code formats the date & show all the parts as two digits
	writedump(dateFormat(now(), 'yy-mm-dd'));
<span class="c">//Day of the week as a three-letter abbreviation.</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s1">&#39;ddd,dd/mm/yyyy&#39;</span><span class="p">));</span>
<span class="c">//Day of the week as its full name.</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s1">&#39;dddd,dd/mm/yyyy&#39;</span><span class="p">));</span>
<span class="c">// the below code formats the month as short string notation</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s1">&#39;yyyy/mmm/dd&#39;</span><span class="p">));</span>
<span class="c">// the below code formats the date &amp; separate date parts with dot</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s1">&#39;yyyy.mmm.dd&#39;</span><span class="p">));</span>
<span class="c">// the below code formats the date &amp; show the full month in string</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nf">now</span><span class="p">(),</span> <span class="s1">&#39;yyyy-mmmm-dd&#39;</span><span class="p">));</span>
<span class="c">// the below code returns the date with the format of full, long, medium, short</span>
<span class="nv">dt</span><span class="o">=</span><span class="nf">createDate</span><span class="p">(</span><span class="m">2018</span><span class="p">);</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nv">dt</span><span class="p">,</span><span class="s2">&quot;full&quot;</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nv">dt</span><span class="p">,</span><span class="s2">&quot;long&quot;</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nv">dt</span><span class="p">,</span><span class="s2">&quot;medium&quot;</span><span class="p">));</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nf">dateFormat</span><span class="p">(</span><span class="nv">dt</span><span class="p">,</span><span class="s2">&quot;short&quot;</span><span class="p">));</span>
<span class="c">//Member function, able to format the date as normal function with all possibilities</span>
<span class="nv">d1</span><span class="o">=</span><span class="nf">createDate</span><span class="p">(</span><span class="m">2018</span><span class="p">,</span><span class="m">07</span><span class="p">,</span><span class="m">25</span><span class="p">)</span>
<span class="nf">writeDump</span><span class="p">(</span><span class="nf">d1.dateFormat</span><span class="p">(</span><span class="s2">&quot;MM/DD/YYYY&quot;</span><span class="p">));</span>
<span class="nf">writeDump</span><span class="p">(</span><span class="nf">d1.dateFormat</span><span class="p">(</span><span class="s2">&quot;dddd,MM/DD/YYYY&quot;</span><span class="p">));</span>

See also