1.2.0 • Published 3 days ago

date-x v1.2.0

Weekly downloads
963
License
MIT
Repository
github
Last release
3 days ago

date-x

Downloads Version@npm Version@git CI

It provides the ability to compare date string with custom date format expression.

Usage

Testing dates with custom date format:

const dateX = require('date-x');
dateX.test("dd/MM/yyyy hh:mm:ss", "03/12/2016 11:11:32"); // true
dateX.test("dd/MM/yyyy hh:mm:ss", "03/12/2016 20:11:32"); // false

Where test is: test(format: string, dateString: string, ignoreCase?: boolean): boolean

Matching date with custom date format:

const dateX = require('date-x');
const m1 = dateX.match("dd/MM/yyyy hh:mm:ss", "03/12/2016 11:11:32");
// m1: [
//   '03/12/2016 11:11:32',
//   '03', '12', '2016',
//   '11', '11', '32',
//   index: 0, ...
// ]
const m2 = datex.match("dd/MM/yyyy hh:mm:ss", "03/12/2016 20:11:32");
// m2: null

Where match is: match(format: string, dateString: string, ignoreCase?: boolean): RegExpMatchArray

Support for localized matching.

CodeName
ENEnglish (default)
HUHungarian
NLDutch
const dateX = require('date-x');
dateX.test("RRRR, EEEE", "Yesterday, Monday"); // true

dateX.loadLocale('hu');
dateX.test("RRRR, EEEE", "Tegnap, Hétfő"); // true

Where loadLocale is: loadLocale(local: "en" | "hu" | "nl"): void

Assertions

const {
    assertDateFormat,
    asssertNotDateFormat
} = require("date-x/assert");

assertDateFormat("03/12/2016 11:11:32", "dd/MM/yyyy hh:mm:ss");
assertNotDateFormat("this is not a date", "yyyy-MM-dd");

Where both assertion has the following parameters:

  1. dateString: string the actual value to check
  2. format: string the date format to match
  3. options?: DateFormatAssertOptions the modifiers/options to match:
    • locale?: "hu" | "en" | "nl" the locale to use
    • ignoreCase?: boolean whether character casing should be ignored

Chai plugin

const chai = require('chai');
chai.use(require('date-x/chai'));

chai.expect("03/12/2016 11:11:32").to.be.inDateFormat("dd/MM/yyyy hh:mm:ss");

Date format string

It based on AngularJS date filter format tokens. The module will replace the tokens in the string in given order of the bellow tokens.

TokenDescriptionExample (EN)
LLLLStand-alone month in yearJanuary-December
MMMMMonth in yearJanuary-December
MMMMonth in yearJan-Dec
MMMonth in year, by number, padded01-12
MMonth in year, by number1-12
yyyy4 digit representation of yearAD 1 => 0001, AD 2010 => 2010
yy2 digit representation of year00-99: AD 2001 => 01, AD 2010 => 10
y1 digit representation of yearAD 1 => 1, AD 199 => 199
QQuarter in year1, 2, 3, 4
EEEEDay in weekMonday-Sunday
EEEDay in weekMon-Sun
RRRRRelative daysToday, Yesterday, Tomorrow
ddDay in month, padded01-31
dDay in month1-31
DoDay in month with ordinal1st, 2nd, 3rd, ...
WWWeek of year, padded01-53
WWeek of year1-53
wwWeek of year, padded01-53
woWeek of year with ordinal1st, 2nd, 3rd, ...
wWeek of year1-53
GGGGStand-alone week of year1-53
ggggWeek of year1-53
HHHour in day, padded00-23
HHour in day0-23
hhHour in AM/PM, padded01-12
hHour in AM/PM1-12
kkHour in day, padded01-24
kHour in day1-24
mmMinute in hour, padded00-59
mMinute in hour0-59
sssMillisecond in second, padded000-999
ssSecond in minute, padded00-59
sSecond in minute0-59
XUnix timestamp1360013296
xUnix timestamp (milliseconds)1360013296123
aAM/PM markAM, PM
Z4 digit (+sign) representation of the timezone offet-1200 - +1200
zzzTimezone abbreviationEastern Standard Time, Pacific Daylight Time
zzTimezone abbreviationEST, MDT

It also supports predefined localizable formats, like: short , medium , fullDate , mediumTime , etc. in all supported locales.

Important

Using the above-listed tokens as normal characters can be done with using the ! character in fron of a character. For example to set an a in the format, use it as !a, so that it won't match for AM/PM.

If you would like to set a ! character in the format, use the !!.

In case of characters what are not listed in the format tokens, no need to use the !.

1.2.0

3 days ago

1.1.0

3 years ago

1.0.0

3 years ago

0.3.0

7 years ago

0.2.0

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.2

8 years ago