Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yousefvand/persian-date-parser
Parser library for date and time. Supports Jalali, Gregorian or mixed.
https://github.com/yousefvand/persian-date-parser
calendar datetime gregorian jalali nodejs parser parsing persian
Last synced: 16 days ago
JSON representation
Parser library for date and time. Supports Jalali, Gregorian or mixed.
- Host: GitHub
- URL: https://github.com/yousefvand/persian-date-parser
- Owner: yousefvand
- License: gpl-3.0
- Created: 2020-08-27T11:20:28.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-27T10:12:22.000Z (about 4 years ago)
- Last Synced: 2024-04-23T05:19:21.803Z (7 months ago)
- Topics: calendar, datetime, gregorian, jalali, nodejs, parser, parsing, persian
- Language: JavaScript
- Homepage:
- Size: 695 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Persian date parser
[![npm version](https://badge.fury.io/js/persian-date-parser.svg)](https://badge.fury.io/js/persian-date-parser) [![GitHub forks](https://img.shields.io/github/forks/yousefvand/persian-date-parser)](https://github.com/yousefvand/persian-date-parser/network) [![GitHub stars](https://img.shields.io/github/stars/yousefvand/persian-date-parser)](https://github.com/yousefvand/persian-date-parser/stargazers) [![GitHub issues](https://img.shields.io/github/issues/yousefvand/persian-date-parser)](https://github.com/yousefvand/persian-date-parser/issues) [![Code coverage](https://img.shields.io/badge/coverage-100%25-blueviolet)](https://github.com/yousefvand/persian-date-parser) [![GitHub license](https://img.shields.io/github/license/yousefvand/persian-date-parser)](https://github.com/yousefvand/persian-date-parser/blob/master/LICENSE)
Library for parsing date and time to desired format (Jalali, Gregorian or mixed).
When possible consider using built-in [Date.prototype.toLocaleDateString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#:~:text=The%20toLocaleDateString()%20method%20returns,the%20behavior%20of%20the%20function.) method.
## Usage
```bash
> npm i persian-date-parser
``````js
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. |```js
const parse = parser() // no caching by default
```or
```js
const parse = parser(1000) // or parser(1000, 'LRU')
``````js
const now = new Date()
```Jalali example:
```js
const format = 'pjyyyy/pjmm/pjdd - pjdddd - pjHH:pjMM pjTT'
const result = parse(format, now)
// result: ۱۳۹۹/۰۶/۰۹ - یکشنبه - ۲۱:۳۶ ب.ظ
```Gregorian Example:
```js
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).
| 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. |