Documentation for the worlds leading Java Based Open Source Web Content Management System

dotCMS

Dates

There are several techniques listed below for returning and handling dates and date formatting. A preview of this entire section is recommended before handling dates in the dotCMS

The basic tooling below returns different messages based upon the input date. The input date must be before the current date.

Macro Overview:

Syntax:

$dateviewapi.friendly(date)

Arguments:

  • date (date/required)
    Date to diff with the current date.

Optional Parameters:

Usage:

Use this to show the difference date and time between a string date (before the current date) and the current date.

WebAPI

java.lang.Object
APIconhttp://www.dotcms.com/docs/1.9/javadocs/com/dotmarketing/util/DateUtil.html

Date Formatting

Useful Examples:

Example 1: A simple example using the required fields

Wed, 02 Apr 2008 16:23:14 +0000
#set($tempDate = $UtilMethods.parseDate("Wed, 02 Apr 2008 16:23:14 +0000", "EEE, dd MMM yyyy HH:mm:ss +SSSS")) Result Message: $dateviewapi.friendly($tempDate)
Assuming that current date is: Fri, 02 May 2008 17:20:00 - The output will be: Wed., April 2, 4:23 PM

Example 2: An example showing how to include one or more of the optional parameters

Sat, 26 Apr 2008 16:23:14 +0000
#set($tempDate = $UtilMethods.parseDate("Sat, 26 Apr 2008 16:23:14 +0000", "EEE, dd MMM yyyy HH:mm:ss +SSSS")) Result Message: $dateviewapi.friendly($tempDate)
Assuming that current date is: Fri, 02 May 2008 17:20:00 - The output will be: Saturday at 4:23 PM

Example 3: An example showing how to include one or more of the optional parameters

Thu, 01 May 2008 16:23:14 +0000
#set($tempDate = $UtilMethods.parseDate("Thu, 01 May 2008 16:23:14 +0000", "EEE, dd MMM yyyy HH:mm:ss +SSSS")) Result Message: $dateviewapi.friendly($tempDate)
Assuming that current date is: Fri, 02 May 2008 17:20:00 - The output will be: Yesterday at 4:23 PM

Example 4: An example showing how to include one or more of the optional parameters

Fri, 02 May 2008 14:20:00 +0000
#set($tempDate = $UtilMethods.parseDate("Fri, 02 May 2008 14:20:00 +0000", "EEE, dd MMM yyyy HH:mm:ss +SSSS")) Result Message: $dateviewapi.friendly($tempDate)
Assuming that current date is: Fri, 02 Apr 2008 17:20:00 - The output will be: 3 hour(s) ago

Example 5: An example showing how to include one or more of the optional parameters

Fri, 02 May 2008 17:10:00 +0000
#set($tempDate = $UtilMethods.parseDate("Fri, 02 May 2008 17:10:00 +0000", "EEE, dd MMM yyyy HH:mm:ss +SSSS")) Result Message: $dateviewapi.friendly($tempDate)
Assuming that current date is: Fri, 02 May 2008 17:20:00 - The output will be: 10 minute(s) ago

Example 6: Date Format- Month:

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

Example 7: Date Format- Year:

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

Example 8: Date Format- MM/DD/YYYY:

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

Example 9: Convert date field to HTML date

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

Example 10: Convert date field to HTML date with formatting

$UtilMethods.dateToHTMLDate($content.gameDatetime.toString(),'EEE MMM dd hh:mm:ss z yyyy','M/d/yyyy') Eg: 7/6/2006
$UtilMethods.dateToHTMLDate($content.gameDatetime.toString(),'EEE MMM dd hh:mm:ss z yyyy','hh:mm:ss a') Eg: 11:30 AM

Example 11: Pull Date Fields, format and convert to integer and compare

#set($dateMonth = $date.format('MMMM', $content.date))    
#set($datePYear = $date.format('yyyy', $content.date))
#if($math.toInteger($datePYear) == $math.toInteger($dateYear))
#if(!$dateMonth.equals($currentMonth))
#set($currentMonth = $dateMonth)
   

Example 12: Pull the most current folder by year

#set($dateYear = $date.get('yyyy'))
#set($folder=$render.eval('/alumni/galleries/$dateYear/'))
#photoGallery('${folder}')
2

Subscribe to dotcms

Visit this group