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).
#set ($dateRevert = - 1 ) #set ($publishedDate = $date.format('yyyyMMdd' ,$UtilMethods.addDays($UtilMethods.now(), $dateRevert))) #set ($publishedDate = "+YOURSTRUCTURENAME.publishedDate:[0000000 TO $publishedDate]" ) #set ($articleList = $dotcontent.pull("+structureName:YOURSTRUCTURENAME $!{publishedDate}" , "10" , "" )) You could also use the following method.
$date.format( 'MM/dd/yyyy' , $content.endSeason) #set($dateYear = $date.get('yyyy' )) #set($nextYear = $webapi.parseInt($dateYear) + 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.
$UtilMethods.now() getDate() $date.getDate()
Return calendar objects
Return a Calendar object representing the time at which this method was invoked:
getCalendar() $date.getCalendar()
##The code above will give you a list of calendar items that you may
##wish to use to call them put the variable after the call like the following:
$date.getCalendar().time
Format a date
Return a formatted string representing the specified date:
format() ##Example: To output a date in the following format 27 / 01 / 1945 #set ($toDay = ${date.format('dd/mm/yyyy' , $date.getDate())})
Return a Date object representing the specified date:
The example shows how to search for a range of dates in a query for dotCMS (See example above too)
#set ($contentDeadline = ${date.format( 'yyyyMMdd' , $content.deadline)}) #set ($toDay = ${date.format('yyyyMMdd' , $date.getDate())}) #if ($math.toInteger($contentDeadline) > $math.toInteger($toDay)) #set ($now = $date.getDate()) #set ($farBack = $UtilMethods.addDays($now,$dateParameter)) #set ($monthEvent = $date.format('MM' , $farBack)) #set ($yearEvent = $date.format('yyyy' , $farBack)) #set ($dayEvent = $date.format('dd' , $farBack)) #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.
#set ($toDay = ${date.format( 'HH:mma, dd mmm yyyy' , $date.getDate())}) ##12 :56am GMT, 12 October 2012 #set ($datePosted = ${date.format('hh:mma z, dd MMMM yyyy' , $job.publishedDate)})
<span class = "meta author" >Job posted by: <a href= "#" >$job.organisation</a> -
$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.
EEEE - Full day name (Friday) EEE - Short day name (FRI) HH:mm - 12 hour clock ( 13 : 00 ) hh:mm - 24 hour clock ( 1 : 00 ) a - Shows am/pm (am) mm - Minutes (31 ) DD - Day (12 ) MMM - Month (12 ) yyyy - Full year (1985 ) yy - Shortend year (85 ) G - era designator (AD) y - year (1996 ) M - month in year (July & 07 ) d - day in month (10 ) h - hour in am/pm (1 ~ 12 ) ( 12 ) H - hour in day (0 ~ 23 ) ( 0 ) m - minute in hour (30 ) s - second in minute (55 ) S - millisecond (978 ) E - day in week (Tuesday) D - day in year (189 ) F - day of week in month 2 (2nd Wed in July) w - week in year (27 ) W - week in month (2 ) k - hour in day (1 ~ 24 ) ( 24 ) K - hour in am/pm (0 ~ 11 ) ( 0 ) z - time zone (Pacific Standard Time) ' - escape for text '' - single quote (')
Comparing Dates How to compare dates:
##Today's Date $date.format('yyyyMMdd' ,$date) $date = Jan 31 , 2013 10 : 18 : 48 AM $date.long = January 31 , 2013 10 : 18 : 48 AM EST $date.medium_time = 10 : 18 : 48 AM $date.full_date = Thursday, January 31 , 2013 $date.get('default' , 'short' ) = Jan 31 , 2013 10 : 18 AM $date.get('yyyy-M-d H:m:s' ) = 2013 - 1 - 31 10 : 18 : 48 #foreach($con in $dotcontent.pull("+structureName:TestDates" , 10 , "modDate desc" )) $con.title #set($strDate1=$date.format('MM/dd/yyyy hh:mm:ss a' ,$con.date1)) #set($strDate2=$date.format('MM/dd/yyyy hh:mm:ss a' ,$con.date2)) Date1=$strDate1 Date2=$strDate2 #set($cal1=$date.toCalendar($con.date1)) #set($cal2=$date.toCalendar($con.date2)) #set($calToday=$date.toCalendar($date)) #set($calCompare=$cal1.compareTo($cal2)) #set($cal1CompareToday=$cal1.compareTo($calToday)) #set($cal2CompareToday=$cal2.compareTo($calToday)) #if ($calCompare== 0 ) $strDate1 equals $strDate2 #elseif($calCompare>0 ) $strDate1 is greater than $strDate2 #else $strDate1 is less than $strDate2 #end #if ($cal1CompareToday> 0 ) $strDate1 is in the future #else $strDate1 is in the past #end #if ($cal2CompareToday> 0 ) $strDate2 is in the future #else $strDate2 is in the past #end #end
Convert date field to HTML date This function, used rarely will turn a string into the date format.
$webapi.dateToHTML($UtilMethods.htmlToDate($Posting_Date), 'MM/dd/yy' ) $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
Please enable JavaScript to view the comments powered by Disqus.
comments powered by