📅  最后修改于: 2023-12-03 15:15:54.284000             🧑  作者: Mango
As a programmer, you might have come across using the Gregorian calendar in your code to get the current date and time. However, in some cases, you may need to use the Jalali calendar, which is mainly used in Iran and Afghanistan. If that is the case, then Jalali Moment is the solution for you!
Jalali Moment is a Javascript library that helps you work with the Jalali calendar. It provides you with the capability to convert the Gregorian date and time to the Jalali format and vice versa. One of the essential features of this library is that it allows you to get the milliseconds of a given date and time in the Jalali format.
To start using Jalali Moment, you need to include the library in your HTML file. You can download it from the official website or use a Content Delivery Network (CDN). Here is an example of how to include the library using CDN:
<script src="https://cdn.jsdelivr.net/npm/jalali-moment@3.3.4/dist/jalali-moment.min.js"></script>
Before we can get the milliseconds of a given date and time in Jalali, we need to convert it to the Jalali format. Here is an example of how to convert the current Gregorian date and time to Jalali format:
let dateTime = new Date();
let jalaliDateTime = moment(dateTime).locale('fa').format('YYYY/MM/DD HH:mm:ss');
console.log(jalaliDateTime);
In the code snippet above, we first create a new Date object to get the current date and time. We then use the moment library to convert the Gregorian date and time to Jalali format. We specify the locale as 'fa', which is the language code for Persian (Farsi), the official language of Iran. Finally, we format the date and time string to 'YYYY/MM/DD HH:mm:ss' to match the desired format.
The output of the console.log() function should look something like this:
1400/06/26 12:20:43
Now that we have converted the Gregorian date and time to Jalali format, we can easily get the milliseconds of a given Jalali date and time. Here is an example:
let jalaliDateTime = '1400/06/26 12:20:43';
let milliseconds = moment(jalaliDateTime, 'jYYYY/jMM/jDD HH:mm:ss').valueOf();
console.log(milliseconds);
In the code snippet above, we have assigned the Jalali date and time string to the jalaliDateTime variable. We then use the moment library to convert the Jalali date and time to milliseconds. We specify the format of the date and time string as 'jYYYY/jMM/jDD HH:mm:ss', where 'j' is used instead of 'Y', 'M', 'D', and 'H' to indicate that we are working with the Jalali calendar. Finally, we use the valueOf() function to get the milliseconds.
The output of the console.log() function should look something like this:
1632722443000
Jalali Moment is a powerful Javascript library that allows you to work with the Jalali calendar. It provides you with numerous features, such as converting Gregorian date and time to Jalali format and vice versa, getting the milliseconds of a given Jalali date and time, and much more. By using Jalali Moment, you can easily incorporate the Jalali calendar into your code and make your application more user-friendly for users in Iran and Afghanistan.