PersianDate
javascript date library for parsing, validating, manipulating, and formatting persian dates System.
Install / Use
/learn @babakhani/PersianDateREADME
Persian Date
Javascript date library for parsing, validating, manipulating, and formatting Persian dates System.
from 1.0.0 persian date support gregorian calendar.
Inspired by momentjs
More info at wikipedia
<a class="github-button" href="https://github.com/babakhani/persiandate" data-icon="octicon-star" data-style="mega" data-count-href="/babakhani/persiandate/stargazers" data-count-api="/repos/babakhani/persiandate#stargazers_count" data-count-aria-label="# stargazers on GitHub" aria-label="Star babakhani/persiandate on GitHub">Star</a> <a class="github-button" href="https://github.com/babakhani/persiandate/issues" data-icon="octicon-issue-opened" data-style="mega" data-count-api="/repos/babakhani/persiandate#open_issues_count" data-count-aria-label="# issues on GitHub" aria-label="Issue babakhani/persiandate on GitHub">Issue</a> <a class="github-button" href="https://github.com/babakhani/persiandate/fork" data-icon="octicon-repo-forked" data-style="mega" data-count-href="/babakhani/persiandate/network" data-count-api="/repos/babakhani/persiandate#forks_count" data-count-aria-label="# forks on GitHub" aria-label="Fork babakhani/persiandate on GitHub">Fork</a>
Install
npm install persian-date --save-dev
bower install persian-date --save-dev
Browser
<script src="node_modules/persian-date/dist/persian-date.js" type="text/javascript"></script>
<script type="text/javascript">
new persianDate().format(); // "۱۳۹۶-۰۱-۱۱ ۲۳:۳۳:۲۷ ب ظ" (when i run in my console)
</script>
Webpack
require('persian-date');
Calendar and locale
toCalendar
default: persian
available option: persian gregorian
from version 1.0.* persianDate have an option that allows developers to set calendar type of Date object.
you can change calendar type globally or only in specific object
if you want change calendar type globally:
persianDate.toCalendar('gregorian');
new persianDate([2017]).year(); // 2017
new persianDate([2017]).format('MMM'); // "ژانویه"
or only on instance:
new persianDate([1396]).toCalendar('gregorian').year(); // 2017
toLeapYearMode
default: algorithmic
available option: algorithmic, astronomical
There is two popular way to determining leap years for the Persian calendar.
-
astronomical: occur whenever that number of days elapsed between equinoxes at the reference meridian.
-
algorithmic: based on Behrooz-Birashk proposed algorithm.
After version 1.0.* persianDate support both algorithms and you can choose which algorithm use in your project.
currently, we have support 2 type of leap year mode algorithmic, astronomical.
you can change it globally like this example
persianDate.toLeapYearMode('algorithmic')
or change it in you instance
new persianDate().toLeapYearMode('algorithmic')
toLeapYearModeonly work when calendar type ispersian, and doesnt any effect ongregoriancalendar
toLocale
default: fa
available option: fa en
if you want change locale globally:
persianDate.toLocale('fa');
new persianDate([1396,6,17]).format(); // "۱۳۹۶-۰۶-۱۷ ۰۰:۰۰:۰۰ ق ظ"
new persianDate([1396,6,17]).format('dddd'); // "جمعه"
new persianDate([1396,6,17]).format('MMMM'); // "شهریور"
persianDate.toLocale('en');
new persianDate([1396,6,17]).format(); // "1396-06-17 00:00:00 AM"
new persianDate([1396,6,17]).format('dddd'); // "Friday"
new persianDate([1396,6,17]).format('MMMM'); // "Shahrivar"
or only on instance:
new persianDate([1396,6,17]).toLocale('fa').format(); // "۱۳۹۶-۰۶-۱۷ ۰۰:۰۰:۰۰ ق ظ"
new persianDate([1396,6,17]).toLocale('fa').format('dddd'); // "جمعه"
new persianDate([1396,6,17]).toLocale('fa').format('MMMM'); // "شهریور"
persianDate.toCalendar('gregorian');
new persianDate([1396,6,17]).toLocale('en').format(); // "1397-07-07 00:00:00 AM"
new persianDate([1396,6,17]).toLocale('en').format('dddd'); // "Friday"
new persianDate([1396,6,17]).toLocale('en').format('MMMM'); // "June"
after version 1.0.*, you must use
toLocaleinsteadformatPersian, for show persian or english digit.
Parse
Instead of modifying the native Date.prototype , persianDate creates a wrapper for the Date object.
To get this wrapper object, simply call persianDate() with one of the supported input types.
Now
new persianDate();
To get the current date and time, just call persianDate() with no parameters.
var now = new persianDate();
This is essentially the same as calling new persianDate(new Date()) .
Unix Offset (milliseconds)
new persianDate(/* Number */);
Similar to new Date(Number), you can create a persianDate by passing an integer value representing the number of milliseconds since the Unix Epoch (Jan 1 1970 12AM UTC).
var day = new persianDate(1318781876406); // "۱۳۹۰-۰۷-۲۴ ۱۹:۴۷:۵۶ ب ظ"
Unix Timestamp (seconds)
persianDate.unix(/* Number */);
To create a persianDate from a Unix timestamp (seconds since the Unix Epoch), use persianDate.unix(Number)
var day = new persianDate.unix(1318781876); // "۱۳۹۰-۰۷-۲۴ ۱۹:۴۷:۵۶ ب ظ"
This is implemented as persianDate(timestamp * 1000) , so partial seconds in the input timestamp are included.
Date
<!-- skip-example -->new persianDate(new Date());
You can create a persianDate with a pre-existing native Javascript Date object.
var day = new Date(2011, 9, 16);
var dayWrapper = new persianDate(day); // "۱۳۹۰-۰۷-۲۴ ۰۰:۰۰:۰۰ ق ظ"
This is the fastest way to get a persianDate.js wrapper.
Array
['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond']
new persianDate([1391, 12, 29, 12, 25, 25, 900]);
You can create a persianDate with an array of numbers that mirror the parameters passed to new Date() But As Persian Date Number Like [1393,2,22,11,22,30]
new persianDate([1393, 1, 14, 15,25, 50,125]); // "۱۳۹۳-۰۱-۱۴ ۱۵:۲۵:۵۰ ب ظ"
Any value past the year is optional, and will default to the lowest possible number.
new persianDate([1392]); // Farvardin 1st
new persianDate([1392, 6]); // Shahrivar 1st
new persianDate([1392, 6, 10]); // Shahrivar 10th
Note: from 1.0.* you can pass gregorian date array to create gregorian date object. for this functionality you must change date object calendar type by
toCalendar('gregorian')
example:
persianDate.toCalendar('gregorian');
new persianDate([2017,2,2]).format(); // "۲۰۱۷-۰۲-۰۲ ۰۰:۰۰:۰۰ ق ظ"
ASP.NET JSON Date
new persianDate(String);
ASP.NET returns dates in JSON as /Date(1198908717056)/ or /Date(1198908717056-0700)/
If a string that matches this format is passed in, it will be parsed correctly.
new persianDate("/Date(1198908717056-0700)/"); //"۱۳۸۶-۱۰-۰۸ ۰۹:۴۱:۵۷ ق ظ"
PesianDate Clone
new persianDate(persianDate);
All persianDate are mutable. If you want a clone of a persianDate, you can do so explicitly or implicitly.
Calling persianDate() on a persianDate will clone it.
var a = new persianDate([1392]);
var b = new persianDate(a);
a.year(1300);
b.year(); // 1392
<!-- skip-example -->
var a = new persianDate([1392]);
var b = a.clone();
a.year(1300);
b.year(); // 1392
Get + Set
persainDate.js uses overloaded getters and setters. You may be familiar with this pattern from it's use in jQuery.
Calling these methods without parameters acts as a getter, and calling them with a parameter acts as a setter.
These map to the corresponding function on the native Date object.
new persianDate().seconds(30).valueOf() === new Date().setSeconds(30); // true
new persianDate().seconds() === new Date().getSeconds(); // true
Millisecond
new persianDate().millisecond(100);
new persianDate().millisecond(); // 100
new persianDate().milliseconds(100);
new persianDate().milliseconds(); // 100
Gets or sets the milliseconds.
Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the seconds.
Second
new persianDate().second(10);
new persianDate().second(); // 10
new persianDate().seconds(10);
new persianDate().seconds(); // 10
Gets or sets the seconds.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the minutes.
Minute
new persianDate().minute(20);
new persianDate().minute(); // 20
new persianDate().minutes(20);
new persianDate().minutes(); // 20
Gets or sets the minutes.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the hours.
Hour
new persianDate().hour(12);
new persianDate().hour(); // 12
new persianDate().hours(12);
new persianDate().hours(); // 12
Gets or sets the hour.
Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the day.
Date of Month
new persianDate().date(23);
new persianDate().date(); // 23
new persianDate().dates(23);
new persianDate().dates()
