Format a date

Return a formatted string representing the specified date:

  1. format()  
  2.   
  3. ##Example: To output a date in the following format 27/01/1945   
  4.   
  5. #set ($toDay = ${date.format('dd/mm/yyyy', $date.getDate())})  

Return a Date object representing the specified date:

  1. toDate()  

The example shows how to search for a range of dates in a query for dotCMS (See example above too)

  1. #set ($contentDeadline = ${date.format('yyyyMMdd', $content.deadline)})  
  2. #set ($toDay = ${date.format('yyyyMMdd', $date.getDate())})  
  3. #if($math.toInteger($contentDeadline) > $math.toInteger($toDay))  
  4.   
  5. #set ($now = $date.getDate())  
  6. #set ($farBack = $UtilMethods.addDays($now,$dateParameter))  
  7. #set ($monthEvent = $date.format('MM', $farBack))  
  8. #set ($yearEvent = $date.format('yyyy', $farBack))  
  9. #set ($dayEvent = $date.format('dd', $farBack))  
  10. #set ($query_date = "+Job.postedDate:[${yearEvent}${monthEvent}${dayEvent} TO 99999999]")  

The example below shows how to format dates, and display them in the format you want. Please also refer to the section just below this to the list of variables symbols you can use to construct any date format.

  1. #set ($toDay = ${date.format('HH:mma, dd mmm yyyy', $date.getDate())})  
  2.   
  3. ##12:56am GMT, 12 October 2012  
  4. #set ($datePosted = ${date.format('hh:mma z, dd MMMM yyyy', $job.publishedDate)})  
  5.   
  6. <span class="meta author">Job posted by: <a href="#">$job.organisation</a> -
  7.  $datePosted.replaceAll("AM""am").replaceAll("PM""pm")</span>  
comments powered by Disqus