0.2.7 • Published 3 years ago

date-format-parse v0.2.7

Weekly downloads
43,658
License
MIT
Repository
github
Last release
3 years ago

date-format-parse

Lightweight date format and parse. Meant to replace the primary functions of format and parse of momentjs.

NPM

$ npm install date-format-parse --save

Usage

Format

Format Date to String.

import { format } from 'date-format-parse';

format(new Date(), 'YYYY-MM-DD HH:mm:ss.SSS');

// with locale, see locale config below
const obj = { ... }

format(new Date(), 'YYYY-MM-DD', { locale: obj })

Parse

Parse String to Date

import { parse } from 'date-format-parse';

parse('2019-12-10 14:11:12', 'YYYY-MM-DD HH:mm:ss'); // new Date(2019, 11, 10, 14, 11, 12)

// with backupDate, default is new Date()
parse('10:00', 'HH:mm', { backupDate: new Date(2019, 5, 6) }) // new Date(2019, 5, 6, 10)

// with locale, see locale config below
const obj = { ... }

parse('2019-12-10 14:11:12', 'YYYY-MM-DD HH:mm:ss', { locale: obj });

Locale

interface Locale {
  months: string[];
  monthsShort: string[];
  weekdays: string[];
  weekdaysShort: string[];
  weekdaysMin: string[];
  meridiem?: (hours: number, minutes: number, isLowercase: boolean) => string;
  meridiemParse?: RegExp;
  isPM?: (input: string) => boolean;
  ordinal?: () => string;
}

const locale = {
  // MMMM
  months: [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
  ],
  // MMM
  monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  // dddd
  weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  // ddd
  weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  // dd
  weekdaysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  // [A a] format the ampm. The following is the default value
  meridiem: (h: number, m: number, isLowercase: boolean) => {
    const word = h < 12 ? 'AM' : 'PM';
    return isLowercase ? word.toLocaleLowerCase() : word;
  };
  // [A a] used by parse to match the ampm. The following is the default value
  meridiemParse: /[ap]\.?m?\.?/i,
  // [A a] used by parse to determine if the matching string is pm. The following is the default value
  isPM: (input) => {
    return (input + '').toLowerCase().charAt(0) === 'p';
  }
};

Tokens

UintTokenoutput
YearYY70 71 ... 10 11
YYYY1970 1971 ... 2010 2011
MonthM1 2 ... 11 12
MM01 02 ... 11 12
MMMJan Feb ... Nov Dec
MMMMJanuary February ... November December
Day of MonthD1 2 ... 30 31
DD01 02 ... 30 31
Day of Weekd0 1 ... 5 6
ddSu Mo ... Fr Sa
dddSun Mon ... Fri Sat
ddddSunday Monday ... Friday Saturday
AM/PMAAM PM
aam pm
HourH0 1 ... 22 23
HH00 01 ... 22 23
h1 2 ... 12
hh01 02 ... 12
Minutem0 1 ... 58 59
mm00 01 ... 58 59
Seconds0 1 ... 58 59
ss00 01 ... 58 59
Fractional SecondS0 1 ... 8 9
SS00 01 ... 98 99
SSS000 001 ... 998 999
Time ZoneZ-07:00 -06:00 ... +06:00 +07:00
ZZ-0700 -0600 ... +0600 +0700
Week of Yearw1 2 ... 52 53
ww01 02 ... 52 53
Unix TimestampX1360013296
Unix Millisecond Timestampx1360013296123