我熟悉位于 npm 中的一个包,用于将公历日期转换为波斯语 (jalali),但我不知道我应该如何在 ionic 2 angular 2 项目中使用它。
或 Angular 1 的这个包:
是否可以将此包转换为 angular 2?任何的想法?欢迎或教程...
最佳答案
好的,我为此编写了转换器,
首先在你的项目中添加一个provider:
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 中。 谢谢
关于javascript - 将公历日期转换为 Angular 2 和 Ionic 2 中的波斯语(jalali)日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37514454/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的