Simple yet flexible Typescript | Javascript jalali date and time for developers
npm i jdate.js
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"));
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"));
<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>
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)
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
Character | Description | Range | Example |
a | Before noon and afternoon | ق.ظ - ب.ظ | ق.ظ |
b | Numeric representation of a season, without leading zeros | 0-3 | 1 |
d | Day of the month, 2 digits with leading zeros | 01-31 | 13 |
f | Season name | بهار-زمستان | بهار |
g | 12-hour format of an hour without leading zeros | 0-12 | 11 |
h | 12-hour format of an hour with leading zeros | 00-12 | 03 |
i | Minutes with leading zeros | 00-59 | 13 |
j | Day of the month without leading zeros | 1-31 | 4 |
l | A full textual representation of the day of the week | شنبه-جمعه | یکشنبه |
m | Numeric representation of a month, with leading zeros | 01-12 | 02 |
n | Numeric representation of a month, without leading zeros | 1-12 | 2 |
s | Seconds, with leading zeros | 00-59 | 03 |
t | Number of days in the given month | 0-31 | 28 |
u | Millisecond | 000000 | 28 |
v | Short year display in letters | یک-نهصد و نود و نه | نود و هشت | چهارصد و دو |
w | Numeric representation of the day of the week (First day of week is: 0) | 0-6 | 6 |
W | Numeric representation of the day of the week (Javascript default, Like new Date().getDay()) | 1-0 | 6 |
y | A two or three digit representation of a year | 1-999 | 98 | 402 |
A | Before noon and afternoon | بعد از ظهر - قبل از ظهر | قبل از ظهر |
D | Persian ordinal suffix for the day of the month, 2 characters | شن - جم | سه |
F | A full textual representation of a month | فروردین - اسفند | اردیبهشت |
G | 24-hour format of an hour without leading zeros | 0-24 | 3 |
H | 24-hour format of an hour with leading zeros | 00-24 | 03 |
J | Day of the month | یک-سی و یک | سیزده |
L | Whether it’s a leap year | 0-1 | 1 |
M | A short textual representation of a month, two letters | فر-اس | ار |
O | Difference to Greenwich time (GMT) in hours | -1200 - +1400 | +0330 |
V | Full year display in letters | صفر-... | یک هزار و سیصد و نود و هشت |
Y | A full numeric representation of a year, 4 digits | 0-... | 1398 |
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
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) |
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) |
JDate.js is available under the MIT license.