Adding Days and Comparing Dates

Last Updated: Jun 16, 2022
documentation for the dotCMS Content Management System

Date Operations

Today's Date

Here are a few helpful ways to get today's date in velocity and return the value as a string:

Today's date is:
$date.format('yyyyMMdd',$date) = 20241221
$date = Dec 21, 2024, 3:26:19 AM
$date.long = December 21, 2024, 3:26:19 AM EST
$date.medium_time = 3:26:19 AM
$date.full_date = Saturday, December 21, 2024
$date.get('default','short') = Dec 21, 2024, 3:26 AM
$date.get('yyyy-M-d H:m:s') = 2024-12-21 3:26:19

Adding/Subtracting Dates

This code gets a java.util.Calendar and uses it to calculate 24 hours from now:

My date: $date

## Get a java Calendar
#set($x = $date.calendar)

## Add 24 hours (hours=int code 10 - see list below)
$x.add(10,24)

## Show result
My new date :$x.time

Adding and Setting dates

You can also set dates and time with the calendar object. For example, to get a date at midnight one year ago:

My date: $date

## Get a java Calendar
#set($x = $date.calendar)

## Subtract 1 year (year=int code 1 - see list below)
$x.add(1,-1)


## Set the hours, minutes and seconds to 0
$x.set(10,0) ## hours
$x.set(12,0) ## minutes
$x.set(13,0) ## seconds
## Show result in SQL format -
$date.format('yyyy-MM-dd HH:mm:ss',$x.time)

Constants

Here are a list of the constants available in the java calendar that you can use to add or subtract or set (we used 10 in the example above). All constants are declared as public static final int.

java.util.Calendar
ALL_STYLES 0
AM 0
AM_PM 9
APRIL 3
AUGUST 7
DATE 5
DAY_OF_MONTH 5
DAY_OF_WEEK 7
DAY_OF_WEEK_IN_MONTH 8
DAY_OF_YEAR 6
DECEMBER 11
DST_OFFSET 16
ERA 0
FEBRUARY 1
FIELD_COUNT 17
FRIDAY 6
HOUR 10
HOUR_OF_DAY 11
JANUARY 0
JULY 6
JUNE 5
LONG 2
MARCH 2
MAY 4
MILLISECOND 14
MINUTE 12
MONDAY 2
MONTH 2
NOVEMBER 10
OCTOBER 9
PM 1
SATURDAY 7
SECOND 13
SEPTEMBER 8
SHORT 1
SUNDAY 1
THURSDAY 5
TUESDAY 3
UNDECIMBER 12
WEDNESDAY 4
WEEK_OF_MONTH 4
WEEK_OF_YEAR 3
YEAR 1
ZONE_OFFSET 15

Comparison

To compare date strings they first must be converted to a Calendar and then use the compareTo method:

## Converting string date to Calendar
#set($nowDate=$date.toCalendar($date))

## We compare to today's date
#if($calDate.compareTo($date)==0)
Publish Date is today

#elseif($calDate.compareTo($date)>0)
Publish Date is in the future

#else
Publish Date is in the past
#end

The examples above are put together here:

## Add Days to a Date: Sets a start Date 7 days before today.
(You can use a positive or negative number of days days.)
#set($startDate = $date.format('yyyyMMdd', $UtilMethods.addDays($date.date, -7)))
<p>This is the start date string: $startDate</p>

## This returns a string with the numbers in today's date
## Example: "20120823" (Aug 23, 2012)
#set($today = $date.format('yyyyMMdd',$date))

<p>This is today's date string: $today</p>

## Converting string date to Calendar
#set($calDate=$date.toCalendar($con.publishedDate))
#set($nowDate=$date.toCalendar($date))

## We compare to today's date
#if($calDate.compareTo($nowDate)==0)
Publish Date is today

#elseif($calDate.compareTo($nowDate)>0)
Publish Date is in the future

#else
Publish Date is in the past
#end

On this page

×

We Dig Feedback

Selected excerpt:

×