jComponent / Helpers / Date operations
Updated: 18. July 2019
Author: Peter Širka

Date operations

Professional Support Chat with contributors

Here is the list of all registered Date prototypes in jComponent library. You do not need to use moment.js with jComponent.


Method: Date.add()

Method can manipulate with Date.

Date.add(value);
// @name {String}
// return {Date}

console.log(new Date().add('1 day'));
console.log(new Date().add('-1 year'));
console.log(new Date().add('1 week'));

Method: Date.format()

Method can format date. If the format begins with ! exclamation mark the date will be converted to US date. IMPORTANT: format value can contain an environment value e.g. [key].

Date.format(format, [utc]);
// @format {String}
// @utc {Boolean} +v14.2.0 converts date to UTC (default: false)
// return {String}

// iso   - +v17 returns Date in ISO format
// yy    - short years              e.g. "17"
// yyyy  - full years               e.g. "2017"
// M     - short months             e.g. "2"
// MM    - short months             e.g. "02"
// MMM   - called months            e.g. "Sep"
// MMMM  - called months            e.g. "September"
// w     - week                     e.g. "3"
// ww    - week                     e.g. "03"
// D     - short days               e.g. "9"
// DD    - short days               e.g. "09"
// DDD   - called days              e.g. "MO"
// DDDD  - called days              e.g. "Monday"
// H     - short hours              e.g. "1"
// HH    - short hours              e.g. "01"
// m     - short minutes            e.g. "1"
// mm    - short minutes            e.g. "01"
// s     - short seconds            e.g. "4"
// ss    - short seconds            e.g. "04"
// a     - adds AM or PM            e.g. "PM"

console.log(new Date().format('dd.MM.yyyy HH:mm:ss'));  // 25.09.2017 15:16:05
console.log(new Date().format('!HH:mm:ss a'));          // 03:16:05 PM
console.log(new Date().format('ddd dddd'));             // returns name of day: "MO Monday"
console.log(new Date().format('MMM MMMM'));             // returns name of month: "Sep September"
console.log(new Date().format('[date]'));               // reads a format from environments
console.log(new Date().format(null));                   // reads a format from "DEF.dateformat"

Method: Date.toNumber()

+v16 converts date according to the format to a number.

Date.toNumber([format]);
// @format {String} default yyyyMMdd
// returns {Number}

NOW.toNumber();
// Output: 20181022

NOW.toNumber('MMdd');
// Output: 1022