Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angular 2和Ionic 2中,将公历日期转换为波斯语(jalali)日期_Javascript_Ionic Framework_Angular_Ionic2 - Fatal编程技术网

Javascript 在angular 2和Ionic 2中,将公历日期转换为波斯语(jalali)日期

Javascript 在angular 2和Ionic 2中,将公历日期转换为波斯语(jalali)日期,javascript,ionic-framework,angular,ionic2,Javascript,Ionic Framework,Angular,Ionic2,我很熟悉npm中的一个将格里高利日期转换为波斯语(jalali)的包,但我不知道如何在ionic 2和ionic 2项目中使用它 或此软件包适用于angular 1: 是否可以将此软件包转换为angular 2?有什么想法吗?或者欢迎使用教程…好的,我为此编写了convertor 首先在项目中添加提供程序: import {Injectable} from '@angular/core'; @Injectable() export class PersianCalendarService {

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

或此软件包适用于angular 1:


是否可以将此软件包转换为angular 2?有什么想法吗?或者欢迎使用教程…

好的,我为此编写了convertor

首先在项目中添加提供程序:

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;
    }
  }
}
下一步:在@Page部分实现提供者的名称

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

constructor(
   public persianCalendarService: PersianCalendarService) {}
然后只需将日期传递给函数,即可获得Jalali date的良好输出:

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

我将很快在github中添加此代码。 谢谢

将模块用作以下代码

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

您可以通过Jalali日历和公历之间的毫秒差来实现这一点,以下是我的解决方案:

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
你可以很容易地用这种方法把贾拉利转换成格里高利:

g_date_in_milliseconds = j_date_in_milliseconds + difference;
g_date = new Date(g_date_in_milliseconds);
简单地说:

new Date(2019,2,21).toLocaleDateString('fa-Ir');
//输出=>۱۳۹۸/۱/۱


没有必要使用任何软件包。你可以使用这个方法

它的使用非常简单:

var d=新日期();
控制台信息(d.toLocaleDateString(“fa-IR”))
输出类似于
۱۳۹/۳/۱۰

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

const options={year:'numeric',month:'long',day:'numeric'};
控制台信息(d.toLocaleDateString(“fa-IR”,选项))
//输出>输出>输出>输出
注意:如果您将方向更改为
rtl
它看起来很正常

再举一个例子:

const options={weekday:'long',year:'numeric',month:'long',day:'numeric'};
控制台信息(d.toLocaleDateString(“fa-IR”,选项))
//输出>输出>输出>输出>输出>输出>输出
再次:您必须使用
rtl
方向以正确的方式查看它

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

console.info(d.toLocaleDateString(“fa-IR”);
//输出>۱۶:۰:۰
const options={weekday:'long',year:'numeric',month:'long',day:'numeric'};
console.info(d.toLocaleDateString(“fa-IR”,选项));
//输出>输出>输出>输出>输出>输出>输出‏ ۱۶:۰۴:۱۶
这是最后一次!不要忘记方向。rtl


就我而言,它不起作用。因此,我将上面的代码示例更改为“let jalaliDate=moment(新日期('1989/1/24'))。locale('fa')。format('YYYY/M/D');//1367/11/4'我在网上找到了这个答案,大约8个月了,现在又找到了,谢谢程序员先生(亲爱的阿米尔同胞)。这里有一个日期选择器,请随意查看。Tnx阿米尔,你在电报或社交媒体上有社区吗?这是我的电子邮件:rezvani。frontDev@gmail.com和GitHub:GitHub.com/Abolfazl2647/
new Date(2019,2,21).toLocaleDateString('fa-Ir');