将角度2和离子2中的格里高利日期转换为波斯语(jalali)日期

Mor*_*eza 11 javascript ionic-framework ionic2 angular

我熟悉位于npm的一个包用于将格里高利日期转换为波斯语(jalali),但我不知道如何在离子2角2项目中使用它.

贾拉利最新

或角度为1的此包:

ADM-dateTimePicker的

是否可以将此包转换为角度2?任何的想法?或者教程是受欢迎的......

Mor*_*eza 20

好吧,我为此目的写了转换器,

首先在项目中添加提供者:

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

下一步:在您的代码中导入此服务:

import {PersianCalendarService} from '../../providers/persian-calendar-service/persian-calendar-service';
Run Code Online (Sandbox Code Playgroud)

下一步:在@Page部分中实现提供者的名称

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

并在构造函数中

constructor(
   public persianCalendarService: PersianCalendarService) {}
Run Code Online (Sandbox Code Playgroud)

那么只需要将日期传递给函数以获得Jalali日期的良好输出:

 getJalaliDate(date) {
var date1 = this.persianCalendarService.PersianCalendar(date);
this.farsiDate = date1;
Run Code Online (Sandbox Code Playgroud)

}

我很快就会在github中添加这段代码.谢谢


Ami*_*ani 12

简单地:

new Date(2019,2,21).toLocaleDateString('fa-Ir');
Run Code Online (Sandbox Code Playgroud)

// 输出 => ????/?/?


fin*_*ich 6

使用jalali-moment模块作为以下代码

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

演示在朋克

  • 就我而言,它不起作用。所以我将上面的代码示例更改为 'let jalaliDate = moment(new Date('1989/1/24')).locale('fa').format('YYYY/M/D'); // 1367/11/4' (2认同)

Ham*_*adi 6

没有必要使用任何包。您可以使用toLocaleDateString方法。

就这么简单:

var d = new Date();
console.info(d.toLocaleDateString("fa-IR"))
Run Code Online (Sandbox Code Playgroud)

输出将类似于 ????/?/??

但是您可以使用选项来让它更好看!例如:

const options = { year: 'numeric', month: 'long', day: 'numeric' };
console.info(d.toLocaleDateString("fa-IR", options))
// output> ?? ????? ????
Run Code Online (Sandbox Code Playgroud)

注意:如果你改变方向,rtl它看起来很正常!

再举一个例子:

const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.info(d.toLocaleDateString("fa-IR", options))
// output> ???? ????? ??, ????
Run Code Online (Sandbox Code Playgroud)

再说一遍:您必须使用rtl方向才能以正确的方式看到它!

您还可以使用Intl.toLocaleTimeString来显示时间。像这样:

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> ???? ????? ??, ?????? ??:??:??
Run Code Online (Sandbox Code Playgroud)

最后一次!不要忘记rtl方向。


归档时间:

查看次数:

6573 次

最近记录:

5 年,10 月 前