Date Handling

Add and remove days from date

This example shows how to minus 1 day from the current days date. This is used on line 4 where the variable is set to search the YOURSTRUCTURENAME structure based on the published Date where the date starts from the beginning of time to today-1.

You can revert as many days as you like (even get the info from a querystring). The function will also maintain the date structure and time (i.e. 27/01/1985 03:45:00).

  1. #set ($dateRevert = -1)  
  2.   
  3. #set ($publishedDate = $date.format('yyyyMMdd',$UtilMethods.addDays($UtilMethods.now(), $dateRevert)))  
  4. #set ($publishedDate = "+YOURSTRUCTURENAME.publishedDate:[0000000 TO $publishedDate]")  
  5.   
  6. #set ($articleList = $dotcontent.pull("+structureName:YOURSTRUCTURENAME $!{publishedDate}","10",""))   

You could also use the following method.

  1. $date.format('MM/dd/yyyy', $content.endSeason)  
  2. #set($dateYear = $date.get('yyyy'))  
  3. #set($nextYear = $webapi.parseInt($dateYear) + 1)    //returns current year+1  

Return todays date

Use the following snippets to obtain the date. You can see here how to manipulate the date to the format you desire.

  1. $UtilMethods.now()  
  2.   
  3. getDate()  
  4.   
  5. $date.getDate()  

Return calendar objects

Return a Calendar object representing the time at which this method was invoked:

  1. getCalendar()  
  2.   
  3. $date.getCalendar()  
  4.   
  5. ##The code above will give you a list of calendar items that you may 
  6. ##wish to use to call them put the variable after the call like the following:  
  7.   
  8. $date.getCalendar().time  

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>  

List of date variables

Below is a list of date variables to use with date.format as shown above to create a date formatted in the way of your choice.

  1. EEEE - Full day name (Friday)  
  2. EEE - Short day name (FRI)  
  3. HH:mm - 12 hour clock (13:00)  
  4. hh:mm - 24 hour clock (1:00)  
  5. a - Shows am/pm (am)  
  6. mm - Minutes (31)  
  7. DD - Day (12)  
  8. MMM - Month (12)  
  9. yyyy - Full year (1985)  
  10. yy - Shortend year (85)  
  11.   
  12. G - era designator (AD)  
  13. y - year (1996)  
  14. M - month in year (July & 07)  
  15. d - day in month (10)  
  16. h - hour in am/pm (1~12) (12)  
  17. H - hour in day (0~23) (0)  
  18. m - minute in hour (30)  
  19. s - second in minute (55)  
  20. S - millisecond (978)  
  21. E - day in week (Tuesday)  
  22. D - day in year (189)  
  23. F - day of week in month 2 (2nd Wed in July)  
  24. w - week in year (27)  
  25. W - week in month (2)  
  26. k - hour in day (1~24) (24)  
  27. K - hour in am/pm (0~11) (0)  
  28. z - time zone (Pacific Standard Time)  
  29. ' - escape for text  
  30. '' - single quote (')  

Comparing Dates

How to compare dates:

  1. ##Today's Date  
  2.   
  3. $date.format('yyyyMMdd',$date)  
  4. $date = Jan 312013 10:18:48 AM  
  5. $date.long = January 312013 10:18:48 AM EST  
  6. $date.medium_time = 10:18:48 AM  
  7. $date.full_date = Thursday, January 312013  
  8. $date.get('default','short') = Jan 312013 10:18 AM  
  9. $date.get('yyyy-M-d H:m:s') = 2013-1-31 10:18:48  
  10.   
  11. #foreach($con in $dotcontent.pull("+structureName:TestDates",10,"modDate desc"))  
  12.   
  13.     $con.title  
  14.     #set($strDate1=$date.format('MM/dd/yyyy hh:mm:ss a',$con.date1))  
  15.     #set($strDate2=$date.format('MM/dd/yyyy hh:mm:ss a',$con.date2))  
  16.     Date1=$strDate1  
  17.     Date2=$strDate2  
  18.   
  19.     #set($cal1=$date.toCalendar($con.date1))  
  20.     #set($cal2=$date.toCalendar($con.date2))  
  21.     #set($calToday=$date.toCalendar($date))  
  22.     #set($calCompare=$cal1.compareTo($cal2))  
  23.     #set($cal1CompareToday=$cal1.compareTo($calToday))  
  24.     #set($cal2CompareToday=$cal2.compareTo($calToday))  
  25.   
  26.     #if($calCompare==0)  
  27.         $strDate1 equals $strDate2  
  28.     #elseif($calCompare>0)  
  29.         $strDate1 is greater than $strDate2  
  30.     #else  
  31.         $strDate1 is less than $strDate2  
  32.     #end  
  33.   
  34.     #if($cal1CompareToday>0)  
  35.         $strDate1 is in the future  
  36.     #else  
  37.         $strDate1 is in the past  
  38.     #end  
  39.   
  40.     #if($cal2CompareToday>0)  
  41.         $strDate2 is in the future  
  42.     #else  
  43.         $strDate2 is in the past  
  44.     #end  
  45.   
  46. #end  

Convert date field to HTML date

This function, used rarely will turn a string into the date format.

  1. $webapi.dateToHTML($UtilMethods.htmlToDate($Posting_Date),'MM/dd/yy')  

More information on manipulating dates is available here:

http://dotcms.com/docs/latest/VelocityDateAndCalendarTools

Dates - dotCMS Documentation

comments powered by Disqus