Socket
Socket
Sign inDemoInstall

node-date-parser

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-date-parser

Date parser module written in Typescript


Version published
Weekly downloads
2
decreased by-50%
Maintainers
1
Install size
35.9 kB
Created
Weekly downloads
 

Readme

Source

DateParser for node.js

  • Introduction
  • Usage
  • Locales
  • Methods

Introduction

This is a simple date parsing module written in typescript; similar to the date() function of PHP. The module returns a string according to the given formatter string using the given date.

Usage

Install

Install with NPM

npm install node-date-parser

Loading the module

In a Typescript project

import the module then create the parser object.

import DateParser from 'node-date-parser';
parser = new DateParser();

In a Javascript project

require the module

const DateParser = require('node-date-parser').default;
const parser = new DateParser();

// Or do it in one step
const parser = new (require('node-date-parser').default)();

Instantiating the parser

public constructor(locale: string = "en")

The constructor expects 1 conditional argument: the locale. Check the the locale section for the available languages. If this argument is not provided the locale defaults to "en".

If an unknown locale is provided, the constructor will throw an error.

Locales

The parser is shipped with a few locale files which contains translated data. Currently these locales can be used:

  • en: english
  • hu: hungarian

If you would like to use the parser with a different locale you can pass a json file using the loadCustomLocale method.

Feel free to share your translated locale file with me so it can be published with the parser.

Methods

Parse

public parse(format: string, date: Date = new Date()): string

Parameters:

  • format: a date formatter string. Check the reference table below for more information.
  • date: an optional date object to use for the parsing. Defaults to the current date and time.

Format Reference

String formatterDescriptionExample
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, short.mon through sun
EA textual representation of a day, short, capitalized.Mon through Sun
eA textual representation of a day, short, uppercase.MON through SUN
jDay of the month without leading zeros1 to 31
lA full textual representation of the day of the week, lowercase.monday through sunday
KA full textual representation of the day of the week, capitalized.Monday through Sunday
NISO 8601 numeric representation of the day of the week1 (for Monday) through 7 (for Sunday)
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
WISO 8601 week number of year, weeks starting on Monday42 (the 42nd week in the year)
FA full textual representation of a month, such as january or march, lowercase.january through december
fA full textual representation of a month, such as January or March, capitalized.January through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, short, lowercasejan through dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
LWhether it's a leap year1 if it is a leap year, 0 otherwise.
YA full numeric representation of a year, 4 digits1999 or 2003
yA two digit representation of a year99 or 03
aAnte meridiem and Post meridiem, lowercaseam or pm
AAnte meridiem and Post meridiem, capitalizedAm or Pm
BAnte meridiem and Post meridiem, uppercaseAM or PM
cAnte meridiem and Post meridiem, uppercaseante meridiem or post meridiem
CAnte meridiem and Post meridiem, capitalizedAnte meridiem or Post meridiem
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
IMinutes without leading zeros0 to 59
sSeconds with leading zeros.00 through 59
SSeconds without leading zeros.0 through 59
vMilliseconds.15 or 654
VMilliseconds, 3 digits.015 or 654
USeconds since the Unix Epoch1639714248

Example

// 2021-12-01 14:42:39
console.log(dateParser.parse("Y-m-d H:i:s", new Date(2021, 11, 1, 14, 42, 39)));

Load Custom Locale

public loadCustomLocale(customLocaleJson: DateParserLocale): void

If you want to use different languages with the parser which are not shipped with it, you can pass a JSON file using this method. The json must match the DateParserLocale interface.

The method itself doesn't validate the json against the interface!

Keywords

FAQs

Last updated on 30 Apr 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc