Skip to content

rm1dev/JDate.js

Repository files navigation


Simple yet flexible Typescript | Javascript jalali date and time for developers

Downloads Builds Issues Licence

Typescript | Javascript JDate Class ;)

Installation

npm i jdate.js

Usage

Typescript

import 'jdate.js';

let date = new Date();

console.log("Normal format: " + date.echo("Y/m/d"));
console.log("Jalali format: " + date.echoFa("Y/m/d"));

ECMAScript

require('jdate.js');

let date = new Date();

console.log("Normal format: " + date.echo("Y/m/d"));
console.log("Jalali format: " + date.echoFa("Y/m/d"));

Browser

<script src="https://cdn.jsdelivr.net/npm/jdate.js"></script>
<script type="text/javascript">
  (function() {
    var date = new Date();
    console.log("Normal format: " + date.echo("Y/m/d"));
    console.log("Jalali format: " + date.echoFa("Y/m/d"));
  })();
</script>

new Date()

Now with new Date() creates a new gregorian and jalali date object with the current date and time:

var date = new Date();
console.log( date.echoFa() );
console.log( date.echo() );
console.log( date );

~~>    شنبه، 25 دی 1400 - 19:22:22
~~>    Saturday January 15 2022 - 19:22:22
~~>    Sat Jan 15 2022 19:22:22 GMT+0330 (Iran Standard Time)

new Date(...).echo | echoFa

var date = new Date(2019, 4, 3, 10, 33, 30, 0);
console.log( date.echoFa("l، j F Y - H:i:s") );
console.log( date.echo("l، j F Y - H:i:s") );

~~>     جمعه، 13 اردیبهشت 1398 - 10:33:30
~~>     Friday، 3 May 2019 - 10:33:30

Format guide

Character Description Range Example
aBefore noon and afternoonق.ظ - ب.ظق.ظ
bNumeric representation of a season, without leading zeros0-31
dDay of the month, 2 digits with leading zeros01-3113
fSeason nameبهار-زمستانبهار
g12-hour format of an hour without leading zeros0-1211
h12-hour format of an hour with leading zeros00-1203
iMinutes with leading zeros00-5913
jDay of the month without leading zeros1-314
lA full textual representation of the day of the weekشنبه-جمعهیکشنبه
mNumeric representation of a month, with leading zeros01-1202
nNumeric representation of a month, without leading zeros1-122
sSeconds, with leading zeros00-5903
tNumber of days in the given month0-3128
uMillisecond00000028
vShort year display in lettersیک-نهصد و نود و نهنود و هشت | چهارصد و دو
wNumeric representation of the day of the week (First day of week is: 0)0-66
WNumeric representation of the day of the week (Javascript default, Like new Date().getDay())1-06
yA two or three digit representation of a year1-99998 | 402
ABefore noon and afternoonبعد از ظهر - قبل از ظهرقبل از ظهر
DPersian ordinal suffix for the day of the month, 2 charactersشن‍ - جم‍سه
FA full textual representation of a monthفروردین - اسفنداردیبهشت
G24-hour format of an hour without leading zeros0-243
H24-hour format of an hour with leading zeros00-2403
JDay of the monthیک-سی و یکسیزده
LWhether it’s a leap year0-11
MA short textual representation of a month, two lettersفر-اس‍ار
ODifference to Greenwich time (GMT) in hours-1200 - +1400+0330
VFull year display in lettersصفر-...یک هزار و سیصد و نود و هشت
YA full numeric representation of a year, 4 digits0-...1398

Date.parseJalali(...)

You can parse specified shamsi or gregorian date from valid date string to convert it to milliseconds.

Date.parseJalali("1399");                    ~~> 1584662400000 ~~> 1399/01/01 - 03:30:00
Date.parseJalali("1399/02");                 ~~> 1587324600000 ~~> 1399/02/01 - 00:00:00
Date.parseJalali("1399/02/13");              ~~> 1588361400000 ~~> 1399/02/13 - 00:00:00
Date.parseJalali("1399/02/13 03:14:30");     ~~> 1588373070000 ~~> 1399/02/13 - 03:14:30
Date.parseJalali("2019/05/03 01:02:03");     ~~> 1556829123000 ~~> 1398/02/13 - 01:02:03
new Date(
    Date.parseJalali("1993/05/03 03:01:03")
).echoFa("Y/m/d - H:i:s")                   ~~> 1372/02/13 - 03:01:03

Get Jalali Date Methods

These methods can be used for getting information from a date object as a jalali date:

Method Description
getJalaliDate()Get the day as a number (1-31)
getJalaliDay()Get the weekday as a number (sat:1, sun:2, ..., fri: 0)
getJalaliFullYear()Get the year as a four digit number (yyyy)
getJalaliShortYear()Get the year as a two or three digit number (yy | yyy)
getHours()Get the hour (0-23)
getMilliseconds()Get the millisecond (0-999)
getMinutes()Get the minute (0-59)
getJalaliMonth()Get the month as a number (0-11)
getSeconds()Get the second (0-59)
getTime()Get the time (milliseconds)
getTimezone()Difference to Greenwich time (GMT) in hours
getTimezoneOffset()Difference between UTC and Local Time
isJalaliLeapYear()Whether it’s a leap year (0-1)

Set Jalali Date Methods

These methods can be used for set jalali date values (years, months, days, hours, minutes, seconds, milliseconds) for a date object:

Method Description
setJalali(year, [month, date, hours, minutes, seconds])Set jalali date
setHours(hours, min, sec, ms)Set the hour (0-23)
setMilliseconds(ms)Set the milliseconds (0-999)
setMinutes(min, sec, ms)Set the minutes (0-59)
setSeconds(sec, ms)Set the seconds (0-59)
setTime(ms)Set the time (milliseconds)

License

JDate.js is available under the MIT license.