2.1.0 • Published 5 years ago

persian-date-parser v2.1.0

Weekly downloads
1
License
GPL-3.0-or-later
Repository
github
Last release
5 years ago

Persian date parser

npm version GitHub forks GitHub stars GitHub issues Code coverage GitHub license

Library for parsing date and time to desired format (Jalali, Gregorian or mixed).

When possible consider using built-in Date.prototype.toLocaleDateString() method.

Usage

> npm i persian-date-parser
const parser = require('persian-date-parser')

From the above line parser accepts two optional arguments: cache capacity and cache type. Each cache entry has a string (format) as its key (i.e. 'gyyyy-gm-gd') and a function as its value. If in your use-case format strings are limited, parser can benefit from caching those functions. On subsequent parsings for the same format, tokenizing and arranging steps are the same and can be bypassed by using cache, which results in better performance.

Cache CapacityDescription
> 0Any positive number. Bigger numbers mean more memory usage.
0Unlimited caching a.k.a memory leak. Use with caution.
< 0No caching. Default behavior.
Cache TypeEviction Policy
'FIFO'First in first out (default).
'LRU'Least Recently Used.
const parse = parser() // no caching by default

or

const parse = parser(1000) // or parser(1000, 'LRU')
const now = new Date()

Jalali example:

const format = 'pjyyyy/pjmm/pjdd - pjdddd - pjHH:pjMM pjTT'
const result = parse(format, now)
// result: ۱۳۹۹/۰۶/۰۹ - یکشنبه - ۲۱:۳۶ ب.ظ

Gregorian Example:

const format = 'gyyyy/gmm/gdd - gdddd - gHH:gMM gTT'
const result = parse(format, now)
// result: 2020/08/30 - Sunday - 21:36 PM

Masks

Masks are according to following table but there are three general prefixes used with them. Using g or j is mandatory.

  • g: Gregorian date.

  • j: Jalali date.

  • p: Represent results in Persian (numbers, month names, weekdays and their abbreviations).

MaskDescription
dDay of the month as digits. No leading zero for single-digit days.
ddDay of the month as digits. Leading zero for single-digit days.
dddDay of the week as a three-letter abbreviation. Persian -> one-letter.
ddddDay of the week as its full name.
mMonth as digits. No leading zero for single-digit months.
mmMonth as digits. Leading zero for single-digit months.
mmmMonth as a three-letter abbreviation.
mmmmMonth as its full name.
yyYear as last two digits. Leading zero for years less than 10.
yyyyYear represented by four digits.
hHours. No leading zero for single-digit hours (12-hour clock).
hhHours. Leading zero for single-digit hours (12-hour clock).
HHours. No leading zero for single-digit hours (24-hour clock).
HHHours. Leading zero for single-digit hours (24-hour clock).
MMinutes. No leading zero for single-digit minutes.
MMMinutes. Leading zero for single-digit minutes.
sSeconds. No leading zero for single-digit seconds.
ssSeconds. Leading zero for single-digit seconds.
tLowercase, single-character time marker string: a or p.
ttLowercase, two-character time marker string: am or pm.
TUppercase, single-character time marker string: A or P.
TTUppercase, two-character time marker string: AM or PM.
2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago