17

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

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Mort
  • 1,411
  • 5
  • 19
  • 51

5 Answers5

25

Ok, I wrote convertor for this purpose.

First add a provider in your project:

import {Injectable} from '@angular/core';
@Injectable()
export class PersianCalendarService {
  weekDayNames: string[] = ["شنبه", "یکشنبه", "دوشنبه",
    "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه"];
  monthNames: string[] = [
    "فروردین",
    "اردیبهشت",
    "خرداد",
    "تیر",
    "مرداد",
    "شهریور",
    "مهر",
    "آبان",
    "آذر",
    "دی",
    "بهمن",
    "اسفند"];
  strWeekDay: string = null;
  strMonth: string = null;
  day: number = null;
  month: number = null;
  year: number = null;
  ld: number = null;
  farsiDate: string = null;

  today: Date = new Date();

  gregorianYear = null;
  gregorianMonth = null;
  gregorianDate = null;
  WeekDay = null;
  buf1: number[] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
  buf2: number[] = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335];

  constructor() {
  }
  PersianCalendar(gregorianDate): string {
    this.today = gregorianDate;
    this.gregorianYear = this.today.getFullYear();
    this.gregorianMonth = this.today.getMonth() + 1;
    this.gregorianDate = this.today.getDate();
    this.WeekDay = this.today.getDay();
    this.toPersian(gregorianDate);
    return this.strWeekDay + " " + this.day + " " + this.strMonth + " " + this.year;


  }
  toPersian(gregorianDate) {
    if ((this.gregorianYear % 4) != 0)
      this.farsiDate = this.func1();
    else
      this.farsiDate = this.func2();
    this.strMonth = this.monthNames[Math.floor(this.month - 1)];
    this.strWeekDay = this.weekDayNames[this.WeekDay + 1];

  }


  func1(): string {
    this.day = this.buf1[this.gregorianMonth - 1] + this.gregorianDate;
    if (this.day > 79) {
      this.day = this.day - 79;
      if (this.day <= 186) {
        var day2 = this.day;
        this.month = (day2 / 31) + 1;
        this.day = (day2 % 31);
        if (day2 % 31 == 0) {
          this.month--;
          this.day = 31;
        }
        this.year = this.gregorianYear - 621;
      }
      else {
        var day2 = this.day - 186;
        this.month = (day2 / 30) + 7;
        this.day = (day2 % 30);
        if (day2 % 30 == 0) {
          this.month = (day2 / 30) + 6;
          this.day = 30;
        }
        this.year = this.gregorianYear - 621;
      }
    }
    else {
      this.ld = this.gregorianYear > 1996 && this.gregorianYear % 4 == 1 ? 11 : 10;
      var day2 = this.day + this.ld;
      this.month = (day2 / 30) + 10;
      this.day = (day2 % 30);
      if (day2 % 30 == 0) {
        this.month--;
        this.day = 30;
      }
      this.year = this.gregorianYear - 622;
    }
    var fullDate = this.day + "/" + Math.floor(this.month) + "/" + this.year;
    return fullDate
  }



  func2(): string {
    //console.log("entered func2");
    this.day = this.buf2[this.gregorianMonth - 1] + this.gregorianDate;
    this.ld = this.gregorianYear >= 1996 ? 79 : 80;
    if (this.day > this.ld) {
      this.day = this.day - this.ld;
      if (this.day <= 186) {
        var day2 = this.day;
        this.month = (day2 / 31) + 1;
        this.day = (day2 % 31);
        if (day2 % 31 == 0) {
          this.month--;
          this.day = 31;
        }
        this.year = this.gregorianYear - 621;
      } else {
        var day2 = this.day - 186;
        this.month = (day2 / 30) + 7;
        this.day = (day2 % 30);
        if (day2 % 30 == 0) {
          this.month--;
          this.day = 30;
        }
        this.year = this.gregorianYear - 621;
      }
      var fullDate = this.day + "/" + Math.floor(this.month) + "/" + this.year;
      return fullDate
    }
    else {
      var day2 = this.day + 10;
      this.month = (day2 / 30) + 10;
      this.day = (day2 % 30);
      if (day2 % 30 == 0) {
        this.month--;
        this.day = 30;
      }
      this.year = this.gregorianYear - 622;
    }
  }
}

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

Swrena
  • 310
  • 3
  • 12
Mort
  • 1,411
  • 5
  • 19
  • 51
  • See this simple non-library method, it could also help: https://stackoverflow.com/questions/71421825/how-to-convert-persian-jalali-dates-to-other-18-calendar-dates-in-javascript-w – Mohsen Alyafei Mar 12 '22 at 09:38
19

Simply:

new Date(2019,2,21).toLocaleDateString('fa-Ir');

// output => ۱۳۹۸/۱/۱

Pedram
  • 15,766
  • 10
  • 44
  • 73
Amir Rezvani
  • 1,262
  • 11
  • 34
12

Use jalali-moment module as the following code

import * as moment from 'jalali-moment';
let jalaliDate = moment('1989/1/24').locale('fa').format('YYYY/M/D'); // 1367/11/4

demo in plunker

fingerpich
  • 8,500
  • 2
  • 22
  • 32
  • 2
    In my case, it didn't work. So i changed above code sample to 'let jalaliDate = moment(new Date('1989/1/24')).locale('fa').format('YYYY/M/D'); // 1367/11/4' – Rzassar Aug 08 '19 at 10:22
9

There's no need to use any package. You can use toLocaleDateString method.

It's so simple:

var d = new Date();
console.info(d.toLocaleDateString("fa-IR"))

The output will be something like ۱۳۹۹/۳/۱۰

But you can use options to get it a better look! For example:

const options = { year: 'numeric', month: 'long', day: 'numeric' };
console.info(d.toLocaleDateString("fa-IR", options))
// output> ۱۰ خرداد ۱۳۹۹

Note: if you change direction to rtl it looks properly!

And for another example:

const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.info(d.toLocaleDateString("fa-IR", options))
// output> ۱۳۹۹ خرداد ۱۰, شنبه

Again: you must use rtl direction to see it in proper way!

You can also use Intl.toLocaleTimeString for showing time. Like this:

console.info(d.toLocaleDateString("fa-IR"));
// output> ۱۶:۰۶:۰۰

const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.info(d.toLocaleDateString("fa-IR", options));
// output> ۱۳۹۹ خرداد ۱۰, شنبه،‏ ۱۶:۰۴:۱۶

For the last time! Don't forget rtl direction.

Hamidreza
  • 1,465
  • 12
  • 31
  • how can change output to use Gregory number symbols ? – behroozbc Apr 16 '22 at 13:20
  • @behroozbc AFAIK there's no option for that! So you may have to write a function to do so. This answer can help you with the number conversion: https://stackoverflow.com/a/17025392/8342406 – Hamidreza May 18 '22 at 06:08
-1

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);
Vala Khosravi
  • 2,352
  • 3
  • 22
  • 49