Cronus
Javascript Date manipulation and parsing module.
Simplistic, tiny (~5KB gzip) and rather fast library inspired by moment.js and Carbon (PHP). Can be used both as a node.js module and require.js module.
How to use
define('app', ['cronus'], function(cronus) {
// Creates new instance of cronus with date set to crrent moment.
var date = new cronus();
// Parsing will create new instance with set date and time.
// In this case 12th of May, 2014 at current time (not at the beginning of the day).
var dateP = new cronus('2014-05-12 12:05:45');
// Add one day to the instance turning it into '2014-05-13 12:05:45'.
dateP.add(1, 'day');
// Subtract 2 days turning it into '2014-05-11 12:05:45'.
dateP.sub(2, 'day');
// OR
// dateP.sub(2, 'days');
// OR
// dateP.sub(2, 'd');
// Set date to tomorrow - time will be set to the beginning of the day:
// turning it into '2014-05-12 00:00:00'.
dateP.tomorrow();
});
Format
Formatting cronus date instance into the human-readable form.
You can either use format
method without any arguments which will return
date in an ISO format or you can specify your own format.
var date = new cronus();
date.format(); // Will return date in an ISO format: '2014-03-12T15:12:22-08:00'.
date.format('{YYYY}-{MM}-{DD}'); // Specify your own format. Will return '2014-03-12'.
List of special symbols convertable with format:
S
millisecondsSS
milliseconds with leading zeroess
secondsss
seconds with leading zerom
minutesmm
minutes with leading zeroh
hours in AM/PM formathh
hours in AM/PM format with leading zeroH
hours in 00-23 formatHH
hours in 00-23 format with leading zeroa
lower case form of meridiem (AM/PM)A
meridiem in upper case formatD
day of monthDD
day of month with leading zeroDDD
day of year,DDDD
day of year with leading zeroM
month - number starting from 1 to 12MM
month number with leading zero 01 to 12MMM
short name of the monthMMMM
long name of the monthw
day of weekww
day of week with leading zerowww
short name of the weekdaywwww
long name of the weekdayW
week number in the year from 1 to 52 with locale dependancyWW
week number in the year from 01 to 52WWW
week number in the year from 1 to 52 without locate dependencyWWWW
week number in the year with leading zero from 01 to 52 without locate dependencyY
short form of the year - 1995 = 95, 2014 = 14YY
same as aboveYYYY
full yearZ
timezone in a format +01:00 or -08:00ZZ
timezone in a format +0100 -0800
Using list of symbols above, create formats like:
{D} of {MMMM}, {YYYY}
{DD} {MMMM}, {YYYY} - {HH}:{mm}
{DD} {MMMM}, {YYYY} - {HH}:{mm} {Z}
Default ISO-8601 format for dates looks like that:
{YYYY}-{MM}-{DD}T{hh}:{mm}:{ss}{Z}
Formatting symols should always be wrapped in {}
. Anything outside will be
treated as normal text and won't be formatted.
UTC Mode
Cronus has a very useful feature when working with multiple timezones - UTC mode. When this mode is enabled, current datetime object will be altered to show UTC time without any timezone modifications.
Usually, UTC mode is used whenever you need to return correct datestring with format
// Turn new instance into UTC date.
var date = new cronus().utc();
// OR
var date = cronus.utc();
// OR
var date = cronus.utc('2014-05-13 12:05:45');
// To convert current UTC date to the current local time use `local`
date.local();
Diff
You can calculate difference between two dates in various units.
var date1 = new cronus();
var date2 = new cronus().sub(5, 'days');
// Will return difference in milliseconds.
var diff = date1.diff(date2);
// Get difference in seconds.
var diffS = date1.diff(date2, 'seconds'); // Yuo can also use unit aliases such as 's' or 'second'
// Get difference in hours.
var diffH = date1.diff(date2, 'hours'); // Yuo can also use unit aliases such as 'h' or 'hour'
Besides getting difference in one unit type only, you can ask cronus to return an object containing all differences.
var date1 = new cronus();
var date2 = new cronus().sub(5, 'days');
var diff = date1.diff(date2, 'all');
// That will return following object:
/**
{
// Object with rounded absolutely formatted numbers.
relative: {
S: 432000000, // Milliseconds
s: 432000, // Seconds
m: 7200, // Minutes
h: 120, // Hours
d: 5, // days
M: 0, // Months
y: 0 // Years
},
// Strict difference be it posizive or negative numbers.
strict: {
S: 432000000,
s: 432000,
m: 7200,
h: 120,
d: 5,
M: 0.16666...,
y: 0.01369...
}
}
*/