C# 泰国公元2558年至2015年

C# 泰国公元2558年至2015年,c#,datetime,date-format,cultureinfo,C#,Datetime,Date Format,Cultureinfo,如何将日期时间的佛教格式转换为公历格式(例如,泰国2558→ 2015年) DateTime。现在默认使用gregoriacalender生成本地时间。如果要在ThaiBuddhistCalendar中生成该时间,可以通过该Calendar对象的实例获取其值 现在,您可以在中使用此值来生成DateTime 之后,您可以使用特定格式格式化DateTime var now = DateTime.Now; int thaiYear = new ThaiBuddhistCalendar().GetYea

如何将日期时间的佛教格式转换为公历格式(例如,泰国2558→ 2015年)


DateTime。现在
默认使用
gregoriacalender
生成本地时间。如果要在
ThaiBuddhistCalendar
中生成该时间,可以通过该Calendar对象的实例获取其值

现在,您可以在中使用此值来生成
DateTime

之后,您可以使用特定格式格式化
DateTime

var now = DateTime.Now;
int thaiYear = new ThaiBuddhistCalendar().GetYear(now);
int thaiMonth = new ThaiBuddhistCalendar().GetMonth(now);
int thaiDay = new ThaiBuddhistCalendar().GetDayOfMonth(now);
int thaiHour = new ThaiBuddhistCalendar().GetHour(now);
int thaiMinute = new ThaiBuddhistCalendar().GetMinute(now);
int thaiSecond = new ThaiBuddhistCalendar().GetSecond(now);

var thaiDateTime = new DateTime(thaiYear, thaiMonth, thaiDay, thaiHour, thaiMinute, thaiSecond);
Console.WriteLine(thaiDateTime.ToString("yyyy-MM-dd"));
结果将是

2558-05-22

佛历和历法之间有543年的差异 公历。欧洲的2015年是泰国的2558年

如果您正在寻找相反的方法,只需使用一个具有公历日历的区域性,如
InvariantCulture

var today = DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture)); // 2015-05-22
看看这门课

例如,如果您知道泰国年,您可以这样做:

ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
cal.ToDateTime(2558, 2, 1, 0, 0, 0, 0);
.ToString("yyyy-MM-dd") // returns 2015-01-01
ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
DateTime now = DateTime.Now;
DateTime thai = new DateTime(cal.GetYear(now), cal.GetMonth(now), now.Day);
如果需要获取泰语
日期时间
,可以执行以下操作:

ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
cal.ToDateTime(2558, 2, 1, 0, 0, 0, 0);
.ToString("yyyy-MM-dd") // returns 2015-01-01
ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
DateTime now = DateTime.Now;
DateTime thai = new DateTime(cal.GetYear(now), cal.GetMonth(now), now.Day);

您的问题的简单答案是,使用一种
文化
,它使用
gregoriacalendar
作为格式提供程序,就像Soner Gönül在本文末尾所建议的那样

但是,不要尝试调整
日期时间
,使其
年份
属性显示
2558
,而不是
2015

即使
DateTime
没有为存储的日期使用特定的日历,它的属性也始终基于公历给出日期和时间部分。这导致以下情况:

buddhistCalendar.ToDateTime(2558,5,22,0,0,0,0) != new DateTime(2558,5,22,0,0,0,0)
但是:

如果要获取特定日历的给定
DateTime
的年份或任何其他值,请使用该日历的相应方法,如
calendar.GetYear(DateTime)

否则你会被解雇

new DateTime(2558,5,22,0,0,0,0).Year == 2558
但是:


我建议一个简单的方法来解决你的问题

//First, Get your date in long type because we must use long in the next step.
//Or if you have your time in string type just use
//long yourDateTicks = Convert.ToDateTime(yourStringDate).Ticks;

long yourDateTicks = DateTime.Now.Ticks;

//split your DateTime into Day, Month, Year.

int day = new DateTime(yourDateTicks).Day;
int month = new DateTime(yourDateTicks).Month;
int year = new DateTime(yourDateTicks).Year;

//Did you see the type of them above? Of course, It's Integer.
//So you can convert your year into B.C. by simply -543

int bcYear = year - 543;

//Finally, put your items into the blog again and format your desire.

DateTime bcDate = new DateTime(bcYear, month, day);
String bcDateFormat = bcDate.ToString("yyyy-MM-dd");
警告:如果您的当地时间不在泰国,您的年份将是公元前543年

对不起,今天是2558-05-22-543年<代码>日期时间独立于任何日历。调用
DateTime.Now.ToString(“yyyy-MM-dd”,new-CultureInfo(“th”)
将生成“2558-05-22”。@abto No.A
DateTime
默认为公历。甚至说,;DateTime值类型表示日期和时间,其值在公历中的范围为0001年1月1日00:00:00(午夜)(普通纪元)到9999年12月31日(公元前)晚上11:59:59。此
DateTime.Now.ToString(“yyyy-MM-dd”,new CultureInfo(“th”)
生成
2558-05-22
,因为
th
区域性将
ThaiBuddhistCalendar
作为
DateTimeFormatInfo.Calendar
属性。这仅意味着它在公历的该范围内有效。2015年5月22日(公历)与2558年5月22日(泰国佛历)相同,只是另一种表述。您可以通过将
gregorianCalendar.ToDateTime(2015,5,22,0,0,0)
buddhistCalendar.ToDateTime(2558,5,22,0,0,0,0)
@abto进行精确比较来验证这一点。我用不同的方式说几乎相同的事情。
佛历。今天(2558,5,22,0,0,0,0)!=新日期时间(2558,5,22,0,0,0,0,0)
佛历。ToDateTime(2558,5,22,0,0,0,0)=新日期时间(2015,5,22,0,0,0)
//First, Get your date in long type because we must use long in the next step.
//Or if you have your time in string type just use
//long yourDateTicks = Convert.ToDateTime(yourStringDate).Ticks;

long yourDateTicks = DateTime.Now.Ticks;

//split your DateTime into Day, Month, Year.

int day = new DateTime(yourDateTicks).Day;
int month = new DateTime(yourDateTicks).Month;
int year = new DateTime(yourDateTicks).Year;

//Did you see the type of them above? Of course, It's Integer.
//So you can convert your year into B.C. by simply -543

int bcYear = year - 543;

//Finally, put your items into the blog again and format your desire.

DateTime bcDate = new DateTime(bcYear, month, day);
String bcDateFormat = bcDate.ToString("yyyy-MM-dd");