| <!doctype html> |
| <!-- Generated by FreeMarker/Docgen from DocBook --> |
| <html lang="en" class="page-type-section"> |
| <head prefix="og: http://ogp.me/ns#"> |
| <meta charset="utf-8"> |
| <title>Built-ins for dates - FreeMarker Manual</title> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <meta name="format-detection" content="telephone=no"> |
| <meta property="og:site_name" content="FreeMarker Manual"> |
| <meta property="og:title" content="Built-ins for dates"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="http://example.com/ref_builtins_date.html"> |
| <link rel="canonical" href="http://example.com/ref_builtins_date.html"> |
| <link rel="icon" href="favicon.png" type="image/png"> |
| <link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338517553"> |
| </head> |
| <body itemscope itemtype="https://schema.org/Code"> |
| <meta itemprop="url" content="http://example.com/"> |
| <meta itemprop="name" content="FreeMarker Manual"> |
| |
| <!--[if lte IE 9]> |
| <div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div> |
| <![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo"> |
| </a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref.html"><span itemprop="name">Reference</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_builtins.html"><span itemprop="name">Built-in Reference</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_builtins_date.html"><span itemprop="name">Built-ins for dates</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li></ul></div></div></div> <div class="main-content site-width"> |
| <div class="content-wrapper no-toc"> |
| <div id="table-of-contents-wrapper" class="col-left"> |
| </div> |
| <div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="ref_builtins_number.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_builtins_boolean.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="ref_builtins_date" itemprop="headline">Built-ins for dates</h1> |
| </div></div><div class="page-menu"> |
| <div class="page-menu-title">Page Contents</div> |
| <ul><li><a class="page-menu-link" href="#ref_builtin_string_for_date" data-menu-target="ref_builtin_string_for_date">string (when used with a date value)</a></li><li><a class="page-menu-link" href="#ref_builtin_date_datetype" data-menu-target="ref_builtin_date_datetype">date, time, datetime</a></li></ul> </div> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_string_for_date">string (when used with a date value)</h2> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <p>This built-in converts a date to a string, with the specified |
| formatting. (when the default format dictated by the |
| <code class="inline-code">date_format</code>, <code class="inline-code">time_format</code> and |
| <code class="inline-code">datetime_format</code> <a href="ref_directive_setting.html#ref.directive.setting">settings</a> of FreeMarker are |
| good for you, then you do not need this built-in.)</p> |
| |
| <p>The format can be one of the predefined formats, or you can |
| specify the formatting pattern explicitly.</p> |
| |
| <p>The predefined formats are <code class="inline-code">short</code>, |
| <code class="inline-code">medium</code>, <code class="inline-code">long</code>, and |
| <code class="inline-code">full</code> which define how verbose the resulting text |
| will be. For example, if the locale of the output is U.S. English, |
| and the time zone is the U.S. Pacific Time zone, then this:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template">${openingTime?string.short} |
| ${openingTime?string.medium} |
| ${openingTime?string.long} |
| ${openingTime?string.full} |
| |
| ${nextDiscountDay?string.short} |
| ${nextDiscountDay?string.medium} |
| ${nextDiscountDay?string.long} |
| ${nextDiscountDay?string.full} |
| |
| ${lastUpdated?string.short} |
| ${lastUpdated?string.medium} |
| ${lastUpdated?string.long} |
| ${lastUpdated?string.full}</pre></div> |
| |
| <p>will prints something like this:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">12:45 PM |
| 12:45:09 PM |
| 12:45:09 PM CEST |
| 12:45:09 PM CEST |
| |
| 4/20/07 |
| Apr 20, 2007 |
| April 20, 2007 |
| Friday, April 20, 2007 |
| |
| 4/20/07 12:45 PM |
| Apr 20, 2007 12:45:09 PM |
| April 20, 2007 12:45:09 PM CEST |
| Friday, April 20, 2007 12:45:09 PM CEST</pre></div> |
| |
| <p>The exact meaning of <code class="inline-code">short</code>, |
| <code class="inline-code">medium</code>, <code class="inline-code">long</code>, and |
| <code class="inline-code">full</code> depends on the current locale (language). |
| Furthermore, it is specified not by FreeMarker, but the Java |
| platform implementation you run FreeMarker on.</p> |
| |
| <p>For dates that contains both date and time part, you can |
| specify the length of the date and time part independently:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template">${lastUpdated?string.short_long} <#-- short date, long time --> |
| ${lastUpdated?string.medium_short} <#-- medium date, short time --></pre></div> |
| |
| <p>will output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">4/8/03 9:24:44 PM PDT |
| Apr 8, 2003 9:24 PM</pre></div> |
| |
| <p>Note that <code class="inline-code">?string.short</code> is the same as |
| <code class="inline-code">?string.short_short</code>, |
| <code class="inline-code">?string.medium</code> is the same as |
| <code class="inline-code">?string.medium_medium</code>, etc.</p> |
| |
| <div class="callout warning"> |
| <strong class="callout-label">Warning!</strong> |
| |
| <p>Unfortunately, because of the limitations of the Java |
| platform, it can happen that you have date variables in the |
| data-model, where FreeMarker can't decide if the variable stores |
| only date part (year, month, day), only time part (hour, minute, |
| second, millisecond) or both. In this case, FreeMarker don't know |
| how to display the date when you write something like |
| <code class="inline-code">${lastUpdated?string.short}</code> or simply |
| <code class="inline-code">${lastUpdated}</code>, and thus it will stop with |
| error. To prevent this, you can help FreeMarker with the <a href="#ref_builtin_date_datetype"><code>?date</code>, |
| <code>?time</code> and <code>?datetime</code> |
| built-ins</a>. For example: |
| <code class="inline-code">${lastUpdated?datetime?string.short}</code>. Ask the |
| programmer if certain variables of the data-model has this |
| problem, or always use <code class="inline-code">?date</code>, |
| <code class="inline-code">?time</code> and <code class="inline-code">?datetime</code> |
| built-ins.</p> |
| </div> |
| |
| |
| <p>Instead of using the predefined formats, you can specify the |
| formatting pattern explicitly with |
| <code class="inline-code">?string(<em class="code-color">pattern_string</em>)</code>. |
| The pattern uses <a href="http://java.sun.com/j2se/1.4/docs/api/java/text/SimpleDateFormat.html">Java |
| date format syntax</a>. Example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template">${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")} |
| ${lastUpdated?string("EEE, MMM d, ''yy")} |
| ${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}</pre></div> |
| |
| <p>will output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">2003-04-08 21:24:44 Pacific Daylight Time |
| Tue, Apr 8, '03 |
| Tuesday, April 08, 2003, 09:24:44 PM (PDT)</pre></div> |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>Unlike with the predefined formats, you never need to use |
| <code class="inline-code">?date</code>, <code class="inline-code">?time</code> and |
| <code class="inline-code">?datetime</code> with explicitly given patterns, since |
| with the pattern you tell FreeMarker what parts of the date to |
| show. However, FreeMarker will trust you blindly, so you can show |
| "noise" if you display parts that are actually not stored in the |
| variable. For example, <code class="inline-code">${openingTime?string("yyyy-MM-dd |
| hh:mm:ss a")}</code>, where <code class="inline-code">openingTime</code> |
| stores only time, will display <code class="inline-code">1970-01-01 09:24:44 |
| PM</code>.</p> |
| </div> |
| |
| |
| <p>The pattern string also can be <code class="inline-code">"short"</code>, |
| <code class="inline-code">"medium"</code>, ..., <code class="inline-code">"short_medium"</code>, |
| ...etc. These are the same as if you would use the predefined |
| formats with the dot syntax: |
| <code class="inline-code">someDate?string("short")</code> and |
| <code class="inline-code">someDate?string.short</code> are equivalent.</p> |
| |
| <p>See also: <a href="dgui_template_valueinsertion.html#dgui_template_valueinserion_universal_date">the |
| interpolation of dates</a></p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_date_datetype">date, time, datetime</h2> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <p>These built-ins can be used to specify which parts of the date |
| variable are in use:</p> |
| |
| <ul> |
| <li> |
| <p><code class="inline-code">date</code>: Only the year, month and day |
| parts are used.</p> |
| </li> |
| |
| <li> |
| <p><code class="inline-code">time</code>: Only the hour, minute, second and |
| millisecond parts are used.</p> |
| </li> |
| |
| <li> |
| <p><code class="inline-code">datetime</code>: Both the date and the time |
| parts are used.</p> |
| </li> |
| </ul> |
| |
| <p>In optimal case, you do not need to use these built-ins. |
| Unfortunately, because of the technical limitations of the Java |
| platform, FreeMarker sometimes can't find out which parts of the |
| date are in use (i.e. only the year+month+day, or only |
| hour+minute+second+millisecond, or both); ask the programmers which |
| variables has this problem. If FreeMarker has to execute an |
| operation where this information is needed -- such as displaying the |
| date as text -- but it does not know which parts are in use, it will |
| stop with error. This is when you have to use these built-ins. For |
| example, assume <code class="inline-code">openingTime</code> is a such problematic |
| variable:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#assign x = openingTime> <#-- no problem can occur here --> |
| ${openingTime?time} <#-- without ?time it would fail --> |
| <#-- For the sake of better understanding, consider this: --> |
| <#assign openingTime = openingTime?time> |
| ${openingTime} <#-- this will work now --></pre></div> |
| |
| <p>There is another usage of these built-ins: to truncate dates. |
| For example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template">Last updated: ${lastUpdated} <#-- assume that lastUpdated is a date-time value --> |
| Last updated date: ${lastUpdated?date} |
| Last updated time: ${lastUpdated?time}</pre></div> |
| |
| <p>will output something like:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">Last updated: 04/25/2003 08:00:54 PM |
| Last updated date: 04/25/2003 |
| Last updated time: 08:00:54 PM</pre></div> |
| |
| <p>If the left side of the <code class="inline-code">?</code> is string, then |
| these built-ins <a href="ref_builtins_string.html#ref_builtin_string_date">convert |
| strings to date variable</a>.</p> |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_builtins_number.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_builtins_boolean.html"><span>Next</span></a></div></div></div></div> </div> |
| </div> |
| <div class="site-footer"><div class="site-width"><div class="footer-bottom"> <p class="last-generated"> |
| Last generated: |
| <time itemprop="dateModified" datetime="2020-07-09T23:48:37Z" title="Thursday, July 9, 2020 11:48:37 PM GMT">2020-07-09 23:48:37 GMT</time> </p> |
| <p class="copyright"> |
| © <span itemprop="copyrightYear">1999</span>–2020 |
| <a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p> |
| </div></div></div></body> |
| </html> |