Docs > API > Date

PostCMS Date objects are very similar to Date objects in normal JavaScript. For example:
out.write(new Date().getTime());
// => 1223657278130
However, the PostCMS API provides some additional methods that are not part of the standard JavaScript Date library, e.g.
out.write(new Date().format('yyyy-MM-dd'));
// => 2008-10-10
Methods
Date.difference(date1, date2) A string representation of the difference between two date objects.
Date.format(
dateString, formatString)
Format a date string using the format string. The return value is a string, NOT a Date object, e.g.
Date.format("20080808", "yyyy");
// => "2008"
The date string should be in the format "yyyyMMdd" or "yyyyMMddHHmm".
Date.friendlyFormat(
dateString)
A string representaton of the given date-string in an easily-readable format.
Date.friendlyFormat("200810101300");
// => "Today, 1:00PM"
Date.make(dateString [, formatString]) Create a JavaScript Date object from a date string and (optionally) a format string specifying the date string's format. If the format string is not specified, the date string is assumed to be in the format "yyyyMMddHHmm", e.g.
d1 = Date.make('200808081300');
out.writeln(d1);
// => "Fri Aug 08 2008 13:00:00
// GMT+0100 (BST)"

d2 = Date.make('2008','yyyy');
out.writeln(d2);
// => "Tue Jan 01 2008 00:00:00
// GMT-0000 (GMT)"
add(amount, unit) Add the specified number of units to this date object. The valid units are "year", "month", "week", "day"/"date", "hour", and "minute", and the amount may be negative.
d1 = new Date();
d2 = d1.add(-3, "month");
out.write(d2.format('yyyy-MM-dd'));
// => 2008-07-10
difference(otherDate) A string representation of the difference between this date object and another date object.
format(formatString)
A string representation of this date object in the given format.
friendlyFormat() A string representation of this date in an easily-readable format.

Can't find what you're looking for?

Let us know and we'll see if we can help.