将 gregorian 日期转换为 angular 2 和 Ionic 2 中的波斯(jalali)日期
Posted
技术标签:
【中文标题】将 gregorian 日期转换为 angular 2 和 Ionic 2 中的波斯(jalali)日期【英文标题】:Convert gregorian date to persian(jalali) date in angular 2 and Ionic 2 【发布时间】:2016-09-27 14:55:23 【问题描述】:我熟悉位于 npm 中的一个包,用于将公历日期转换为波斯语 (jalali),但我不知道应该如何在 ionic 2 angular 2 项目中使用它。
Jalali-date
或角度 1 的这个包:
ADM-dateTimePicker
是否可以将此包转换为 angular 2?任何想法?欢迎或教程...
【问题讨论】:
【参考方案1】:好的,我为此编写了转换器,
首先在你的项目中添加一个提供者:
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;
下一步:在您的代码中导入此服务:
import PersianCalendarService from '../../providers/persian-calendar-service/persian-calendar-service';
下一步:在 @Page 部分实现提供者的名称
@Page(
templateUrl: 'build/pages/getting-started/getting-started.html',
providers: [PersianCalendarService]
)
在构造函数
中constructor(
public persianCalendarService: PersianCalendarService)
那么您只需将日期传递给函数以获得良好的 Jalali 日期输出:
getJalaliDate(date)
var date1 = this.persianCalendarService.PersianCalendar(date);
this.farsiDate = date1;
我会尽快将这段代码添加到 github 中。 谢谢
【讨论】:
【参考方案2】:使用jalali-moment模块作为如下代码
import * as moment from 'jalali-moment';
let jalaliDate = moment('1989/1/24').locale('fa').format('YYYY/M/D'); // 1367/11/4
demo in plunker
【讨论】:
就我而言,它不起作用。所以我将上面的代码示例更改为 'let jalaliDate = moment(new Date('1989/1/24')).locale('fa').format('YYYY/M/D'); // 1367/11/4'【参考方案3】:您可以通过 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
您可以通过这种技术轻松地将 Jalali 转换为公历:
g_date_in_milliseconds = j_date_in_milliseconds + difference;
g_date = new Date(g_date_in_milliseconds);
【讨论】:
【参考方案4】:简单地说:
new Date(2019,2,21).toLocaleDateString('fa-Ir');
// 输出 => 1398/1/1
【讨论】:
我发现这个答案的所有网络大约 8 个月,现在又找到了,谢谢程序员先生(亲爱的阿米尔同胞)。 这里有反应波斯日期选择器github.com/Abolfazl2647/React-Perisan-Georgian-datepicker随时查看。 Tnx Amir,你在电报或社交媒体上有社区吗? 这是我的电子邮件:rezvani.frontDev@gmail.com 和 GitHub:github.com/Abolfazl2647/【参考方案5】:无需使用任何软件包。您可以使用toLocaleDateString 方法。
就这么简单:
var d = new Date();
console.info(d.toLocaleDateString("fa-IR"))
输出将类似于۱۳۹۹/۳/۱۰
但是您可以使用选项来获得更好的外观!例如:
const options = year: 'numeric', month: 'long', day: 'numeric' ;
console.info(d.toLocaleDateString("fa-IR", options))
// output> ۱۰ خرداد ۱۳۹۹
注意:如果您将方向更改为
rtl
,它看起来正确!
再举一个例子:
const options = weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' ;
console.info(d.toLocaleDateString("fa-IR", options))
// output> ۱۳۹۹ خرداد ۱۰, شنبه
再次声明:您必须使用
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> ۱۳۹۹ خرداد ۱۰, شنبه، ۱۶:۰۴:۱۶
最后一次!不要忘记
rtl
方向。
【讨论】:
以上是关于将 gregorian 日期转换为 angular 2 和 Ionic 2 中的波斯(jalali)日期的主要内容,如果未能解决你的问题,请参考以下文章
javascript 不能正确地将 angular ui datepicker 日期转换为 UTC