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...
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
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
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);