Library for parsing date and time to desired format (Jalali, Gregorian or mixed).
When possible consider using built-in Date.prototype.toLocaleDateString() method.
> 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 Capacity | Description |
---|---|
> 0 | Any positive number. Bigger numbers mean more memory usage. |
0 | Unlimited caching a.k.a memory leak. Use with caution. |
< 0 | No caching. Default behavior. |
Cache Type | Eviction 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 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).
Mask | Description |
---|---|
d | Day of the month as digits. No leading zero for single-digit days. |
dd | Day of the month as digits. Leading zero for single-digit days. |
ddd | Day of the week as a three-letter abbreviation. Persian -> one-letter. |
dddd | Day of the week as its full name. |
m | Month as digits. No leading zero for single-digit months. |
mm | Month as digits. Leading zero for single-digit months. |
mmm | Month as a three-letter abbreviation. |
mmmm | Month as its full name. |
yy | Year as last two digits. Leading zero for years less than 10. |
yyyy | Year represented by four digits. |
h | Hours. No leading zero for single-digit hours (12-hour clock). |
hh | Hours. Leading zero for single-digit hours (12-hour clock). |
H | Hours. No leading zero for single-digit hours (24-hour clock). |
HH | Hours. Leading zero for single-digit hours (24-hour clock). |
M | Minutes. No leading zero for single-digit minutes. |
MM | Minutes. Leading zero for single-digit minutes. |
s | Seconds. No leading zero for single-digit seconds. |
ss | Seconds. Leading zero for single-digit seconds. |
t | Lowercase, single-character time marker string: a or p. |
tt | Lowercase, two-character time marker string: am or pm. |
T | Uppercase, single-character time marker string: A or P. |
TT | Uppercase, two-character time marker string: AM or PM. |