JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 121294
  • Score
    100M100P100Q161596F
  • License MIT

Lightweight date format and parse

Package Exports

  • date-format-parse
  • date-format-parse/lib/locale/af
  • date-format-parse/lib/locale/ar
  • date-format-parse/lib/locale/ar-dz
  • date-format-parse/lib/locale/ar-sa
  • date-format-parse/lib/locale/az
  • date-format-parse/lib/locale/be
  • date-format-parse/lib/locale/bg
  • date-format-parse/lib/locale/bm
  • date-format-parse/lib/locale/bn
  • date-format-parse/lib/locale/ca
  • date-format-parse/lib/locale/cs
  • date-format-parse/lib/locale/cy
  • date-format-parse/lib/locale/da
  • date-format-parse/lib/locale/de
  • date-format-parse/lib/locale/el
  • date-format-parse/lib/locale/en
  • date-format-parse/lib/locale/eo
  • date-format-parse/lib/locale/es
  • date-format-parse/lib/locale/et
  • date-format-parse/lib/locale/fi
  • date-format-parse/lib/locale/fr
  • date-format-parse/lib/locale/gl
  • date-format-parse/lib/locale/gu
  • date-format-parse/lib/locale/he
  • date-format-parse/lib/locale/hi
  • date-format-parse/lib/locale/hr
  • date-format-parse/lib/locale/hu
  • date-format-parse/lib/locale/id
  • date-format-parse/lib/locale/is
  • date-format-parse/lib/locale/it
  • date-format-parse/lib/locale/ja
  • date-format-parse/lib/locale/ka
  • date-format-parse/lib/locale/kk
  • date-format-parse/lib/locale/ko
  • date-format-parse/lib/locale/lt
  • date-format-parse/lib/locale/lv
  • date-format-parse/lib/locale/mk
  • date-format-parse/lib/locale/ms
  • date-format-parse/lib/locale/nb
  • date-format-parse/lib/locale/nl
  • date-format-parse/lib/locale/nl-be
  • date-format-parse/lib/locale/pl
  • date-format-parse/lib/locale/pt
  • date-format-parse/lib/locale/pt-br
  • date-format-parse/lib/locale/ro
  • date-format-parse/lib/locale/ru
  • date-format-parse/lib/locale/sl
  • date-format-parse/lib/locale/sr
  • date-format-parse/lib/locale/sv
  • date-format-parse/lib/locale/ta
  • date-format-parse/lib/locale/te
  • date-format-parse/lib/locale/th
  • date-format-parse/lib/locale/tr
  • date-format-parse/lib/locale/ug-cn
  • date-format-parse/lib/locale/uk
  • date-format-parse/lib/locale/vi
  • date-format-parse/lib/locale/zh-cn
  • date-format-parse/lib/locale/zh-tw

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (date-format-parse) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

date-format-parse

build:passed Badge npm MIT

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

Uint Token output
Year YY 70 71 ... 10 11
YYYY 1970 1971 ... 2010 2011
Month M 1 2 ... 11 12
MM 01 02 ... 11 12
MMM Jan Feb ... Nov Dec
MMMM January February ... November December
Day of Month D 1 2 ... 30 31
DD 01 02 ... 30 31
Day of Week d 0 1 ... 5 6
dd Su Mo ... Fr Sa
ddd Sun Mon ... Fri Sat
dddd Sunday Monday ... Friday Saturday
AM/PM A AM PM
a am pm
Hour H 0 1 ... 22 23
HH 00 01 ... 22 23
h 1 2 ... 12
hh 01 02 ... 12
Minute m 0 1 ... 58 59
mm 00 01 ... 58 59
Second s 0 1 ... 58 59
ss 00 01 ... 58 59
Fractional Second S 0 1 ... 8 9
SS 00 01 ... 98 99
SSS 000 001 ... 998 999
Time Zone Z -07:00 -06:00 ... +06:00 +07:00
ZZ -0700 -0600 ... +0600 +0700
Week of Year w 1 2 ... 52 53
ww 01 02 ... 52 53
Unix Timestamp X 1360013296
Unix Millisecond Timestamp x 1360013296123