Views: 3810
Last Modified: 07.04.2022

JS-library has an core_date.js extension, allowing to format the date.

Connecting in PHP

CJSCore::Init("date");

Call in Javascript

BX.date.format("format", date);

The format is a fully similar to the format function date, except for the format T and e (character names for timezone). Also supports extended formats for function FormatDate.

When specific characters must not be formatted, you need to replace these characters with "slash" characters.

BX.date.format("H:m:s \\m \\i\\s \\m\\o\\n\\t\\h")

When format starts from the symbol ^, the function will crop any zeros. Examples:

15.04.12 13:00:00 => 15.04.12 13:00
00:01:00 => 00:01
4 may 00:00:00 => 4 may
01-01-12 00:00 => 01-01-12

date it's either a timestamp in seconds (Number type), or a Date class object. Otherwise, it's current time (new Date()). Examples:

BX.date.format("d-m-Y H:i:s");
BX.date.format("j F Y H:i:s");
BX.date.format("^d-m-Y H:i:s");
BX.date.format("H:m:s \\m \\i\\s \\m\\o\\n\\t\\h");
BX.date.format("Hago | dago | sago | iago | mago | Yago", new Date(2007, 2, 1, 0, 0, 0));
BX.date.format("sago | iago", 1320271200);

The same as FormatDate, BX.date.format can receive an array with formats for calculating a "1 seconds ago", "2 minutes ago" types and etc.

var format = [
   ["tommorow", "tommorow, H:i:s"],
   ["s" , "sago"],
   ["H", "Hago"],
   ["d", "dago"],
   ["m100", "mago"],
   ["m", "mago"],
   ["-", ""]
];
BX.date.format(format, new Date(2007, 2, 1, 0, 0, 0));

format array consist of elements ["format interval", "format"], where format interval defines to which time interval the format applies.
format array is processed sequentially until the first match.
Time interval is defined between specified date (first parameter in BX.format.date) and current date.

Standard "format interval" values
s up to 60 seconds
i up to 60 minutes
H up to 24 hours
d up to 31 days
m up to 1 year
sN up to N seconds *
iN up to N minutes *
HN up to N hours *
dN up to N days *
mN up to N months *
today today
yesterday yesterday
tommorow tomorrow
- hyphen indicates a future date

* - where N - any positive number.

Example:

var format = [
   ["-", "d.m.Y H:i:s"]
   ["s300" , "sago"],
   ["H", "Hago"],
   ["d", "dago"],
   ["m", "mago"]
];

BX.date.format(format, new Date(2007, 2, 2, 9, 58, 0), new Date(2007, 2, 2, 10, 0, 0)); //1
BX.date.format(format, new Date(2007, 2, 2, 0, 0, 0), new Date(2007, 2, 2, 10, 0, 0)); //2
BX.date.format(format, new Date(2007, 2, 1, 0, 0, 0), new Date(2007, 2, 2, 10, 0, 0)); //3
BX.date.format(format, new Date(2007, 2, 3, 0, 0, 0), new Date(2007, 2, 2, 10, 0, 0)); //4

Third parameter in BX.date.format is a current date (see description below).

  1. "120 seconds ago", triggered ["s300" , "sago"], due to time interval less than 300 seconds
  2. "10 hours ago", triggered ["H", "Hago"], due to time interval less than 24 hours
  3. "1 days ago", triggered ["d", "dago"], due to time interval less than 31 days
  4. "03.03.2007 00:00:00" triggered ["-", "d.m.Y H:i:s"], specified future date

For a default value, the last element can be set as empty "format interval":

var format = [
   ["s" , "sago"],
   ["H", "Hago"],
   ["d", "dago"],
   ["m", "mago"],
   ["", "d.m.Y H:i:s"]
];
BX.date.format(format, new Date(2007, 2, 1, 0, 0, 0));

BX.date.format full signature

BX.date.format("format", date, current time, utc);

current date - date (timestamp in seconds, either Date class object), used for such calculations as "1 seconds ago", "2 years ago". When not specified, default value new Date().

utc - date in UTC. Default value: false. When dates in UTC are required.


Small hint for matching the server and client codes:

time() = new Date()
mktime(...) = new Date(...)
gmmktime(...) = new Date(Date.UTC(...))
mktime(0,0,0, 1, 1, 1970) != 0          new Date(1970,0,1).getTime() != 0
gmmktime(0,0,0, 1, 1, 1970) == 0        new Date(Date.UTC(1970,0,1)).getTime() == 0
date("d.m.Y H:i:s") = BX.date.format("d.m.Y H:i:s")
gmdate("d.m.Y H:i:s") = BX.date.format("d.m.Y H:i:s", null, null, true);

BX.date.convertBitrixFormat

Function converts bitrix-date format into the date function format. Current site date formats can be received as follows:

BX.message("FORMAT_DATE");
BX.message("FORMAT_DATETIME");



Courses developed by Bitrix24