Dates in Velocity

Last Updated: Jun 5, 2023
documentation for the dotCMS Content Management System

At the core of the using Dates in Velocity is the included DateTool, which is automatically mapped to $date in dotCMS. This tool has a number of formatting methods and parsing method.

There is also another Viewtool — DateViewWebAPI, mapped to $dateviewapi — that exposes one valuable method, which is called $dateviewapi.friendly(Date) This method returns friendly interval message based upon the difference between now and the date you pass in as a parameter.

Read more about date formatting and parsing with the DateTool.

Examples

Parse Date from String

#set($tempDate = $date.toDate("EEE, dd MMM yyyy HH:mm:ss +SSSS","Wed, 02 Apr 2008 16:23:14 +0000"))
$date.format('medium',$tempDate) = Wed Apr 02 16:23:14 EDT 2008

Parse Date from String with timezone and offset

#set($tempDate = $date.toDate( "yyyy-MM-dd'T'HH:mm:ss'Z'","2016-05-06T20:42:26Z"))
$tempDate = Fri May 06 20:34:26 EDT 2016

Friendly “since” interval message

#set($tempDate = $date.toDate("EEE, dd MMM yyyy HH:mm:ss +SSSS","Fri, 02 May 2008 14:20:00 +0000"))
$dateviewapi.friendly($tempDate) = 3 hour(s) ago

Formating Dates, assuming it is October 2015

Date Format: Month

$date.format('MMMM', $content.date)) = October

Date Format: Year

$date.format('yyyy', $content.date)) = 2015

Is the content from the current month?

#set($dateMonth = $date.format('MM', $content.publishDate))    
#if($math.toInteger($dateMonth) == $math.toInteger($date.get("MM")))
    #set($currentMonth = true)
#else
    #set($currentMonth = false)
#end
$currentMonth = false

Pull a folder by year and month, assuming it is October 2015

#set($dateYear = $date.get('yyyy'))
#set($dateMonth = $date.get('MM'))
#set($folder="/photo/galleries/$dateYear/$dateMonth")
$folder=/photo/galleries/2015/10

On this page

×

We Dig Feedback

Selected excerpt:

×