2016-05-29 12 views

Antwort

13

ok, schrieb i-Konverter für diesen Zweck,

zunächst einen Anbieter in Ihrem Projekt hinzufügen:

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; 
    } 
    } 
} 

der nächste Schritt: Import dieser Dienst in Ihrem Code:

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

der nächste Schritt: der Name des Providers implementieren in @Page Abschnitt

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

und in Konstruktor

constructor(
    public persianCalendarService: PersianCalendarService) {} 

dann nur müssen Sie das Datum an die Funktion zu übergeben für eine schöne Ausgabe von Jalali Datum bekommen:

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

}

Ich werde diesen Code bald in Github hinzufügen. Dank

+1

gut, aber keine UI für die – ghazyy

+0

tnx seiner Arbeit richtig – Alireza

0

Verwenden jalali-moment Module wie der folgenden Code

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

demo in plunker