Convert gregorian date to persian(jalali) date in angular 2 and Ionic 2

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

I'm familiar with one package located in npm for converting gregorian date to persian (jalali), but i don't know how should i use it in ionic 2 angular 2 projects.

Jalali-date

or this package for angular 1:

ADM-dateTimePicker

is it possible to convert this package to angular 2? any idea? or tutorial are welcome...

回答1:

ok, i wrote convertor for this purpose,

first add a provider in your project:

the next step: import this service in your code:

import {PersianCalendarService} from '../../providers/persian-calendar-service/persian-calendar-service';

the next step: implement the provider's name in @Page section

@Page({   templateUrl: 'build/pages/getting-started/getting-started.html',   providers: [PersianCalendarService] })

and in constructor

constructor(    public persianCalendarService: PersianCalendarService) {}

then just you need to pass the date to the function for getting a nice output of Jalali date:

 getJalaliDate(date) { var date1 = this.persianCalendarService.PersianCalendar(date); this.farsiDate = date1;

}

i'll add this code in github soon. Thanks



回答2:

Use jalali-moment module as the following code

import * as moment from 'jalali-moment'; let jalaliDate = moment("1989/1/24").format('jYYYY/jM/jD'); // 1367/11/4

demo in plunker



回答3:

You can do this by the difference of milliseconds between Jalali calendar and Gregorian, Here's my solution:

var g_date = new Date("2018-04-04 00:00:00"); // example Gregorian date g_date_in_milliseconds = date.getTime(); // Gregorian date in milliseconds const difference =  1.9603638 * Math.pow(10, 13); // difference of Jalali calendar and Gregoria j_date_in_milliseconds = g_date_in_milliseconds - difference; // converted to Jalali milliseconds j_date = new Date(j_date_in_milliseconds); // converted to date object

And you can easily convert Jalali to Gregorian by this technique like this:

g_date_in_milliseconds = j_date_in_milliseconds + difference; g_date = new Date(g_date_in_milliseconds);


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!