[This is a work in progress]
date-slayer is an Intl-API-based native JavaScript solution to common date/time conversion scenarios (mostly around Persian/Jalali Calendar) that are usually handled by various date/time libraries most of which are based on complex solar algorithms & corrections. This one is going to be much more smaller, faster & practical due to skipping implementation details thanks to having EcmaScript's Internationalization API being already implemented by JavaScript runtimes & browsers in hand. So, hopefully this will be a great alternative to all those old solutions based on date-fns
or momentjs
or dayjs
, etc.
converts a Gregorian date into a Jalali object which represents equivalent Jalali date/time details.
import { getJalaliFromGregorian } from 'date-slayer';
const date_2022_04_21 = new Date(1650534589167);
const date = getJalaliFromGregorian(date_2022_04_21);
console.log(date.fa.date) // -> '۱۴۰۱/۰۲/۰۱'
console.log(date.fa.year) // -> '۱۴۰۱'
console.log(date.fa.month) // -> '۰۲'
console.log(date.fa.day) // -> '۰۱'
console.log(date.fa.weekday) // -> 'پنجشنبه'
console.log(date.fa.monthLong) // -> 'اردیبهشت'
// all 'fa' methods in this package have equivalent 'en' methods as well
console.log(date.en.date) // -> '1401/02/01'
console.log(date.en.year) // -> '1401'
console.log(date.en.month) // -> '02'
console.log(date.en.day) // -> '01'
console.log(date.en.weekday) // -> 'Panjshanbeh'
console.log(date.en.monthLong) // -> 'Ordibehesht'
console.log(date.fa.getDescriptiveDate()) // -> 'پنجشنبه ۱ اردیبهشت'
console.log(date.fa.getDescriptiveDate({ year: true })) // -> 'پنجشنبه ۱ اردیبهشت ۱۴۰۱'
console.log(date.fa.getTime()) // -> '۱۴:۱۹'
console.log(date.fa.getTime('HH:mm')) // -> '۱۴:۱۹'
console.log(date.fa.getTime('HH:mm:ss')) // -> '۱۴:۱۹:۴۹'
console.log(date.fa.getTime('hh:mm a')) // -> '۰۲:۱۹ ب.ظ'
console.log(date.fa.getTime('hh:mm:ss a')) // -> '۰۲:۱۹:۴۹ ب.ظ'
see the tests folder for more detailed examples.
Please feel free to open an issue or create a pull request to add a new feature or fix a bug. (see contributing for more details)
The MIT License (MIT)
© 2022 Sina Khodabandehloo