泰国2558年到2015年

时间:2015-05-22 09:55:47

标签: c# datetime date-format cultureinfo

如何将datetime的佛教格式转换为格里高利格式(例如泰国2558 - > 2015)?

using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ThailandBuddhistCalendarTest
{
    class Program
    {
        private const string DateFormat = "yyyy-MM-dd";
        private const string DateTimeOffsetFormat = "yyyy-MM-dd'T'HH:mm:sszzz";

        static void Main(string[] args)
        {
            var ci = new CultureInfo("th-TH");
            var today = DateTime.Now.ToString(DateFormat, ci); // today == "2558-05-22"
        }
    }
}

4 个答案:

答案 0 :(得分:1)

默认情况下,

DateTime.Now会使用GregorianCalender生成您的本地时间。如果您想在ThaiBuddhistCalendar中生成该时间,则可以使用该日历对象的实例获取该值。

现在,您可以在DateTime(Int32, Int32, Int32, Int32, Int32, Int32) constructor中使用此值来生成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

来自http://www.thaiworldview.com/feast/feast.htm

  

佛教历法与佛教历法有543年的差异   阳历日历。 2015年在欧洲是泰国的2558年。

如果您正在寻找相反的方式,只需使用具有格里高利日历的文化作为InvariantCulture部分var today = DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture)); // 2015-05-22

<!doctype html>
<html>
<head>
  <title>Layout Experiments</title>
  <style>
    *{
      -webkit-box-sizing :border-box;
      -moz-box-sizing    :border-box;
      box-sizing         :border-box;
    }
    div{
      max-width      :300px;
      line-height    :1.5;
      text-transform :uppercase;
      height         :200px;
      background     :wheat;
    }
    span{
      background-color :white;
      box-shadow       :5px 5px 0 black;
      padding          :0;
    }
  </style>
</head>
<body>
<div>
  <span>HTML Text block - HTML Text block HTML Text block - HTML Text block</span>
</div>
</body>
</html>

答案 1 :(得分:1)

查看ThaiBuddhistCalendar课程。

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

ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
cal.ToDateTime(2558, 2, 1, 0, 0, 0, 0);
.ToString("yyyy-MM-dd") // returns 2015-01-01

如果你需要获得泰语DateTime,你可以这样做:

ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
DateTime now = DateTime.Now;
DateTime thai = new DateTime(cal.GetYear(now), cal.GetMonth(now), now.Day);

答案 2 :(得分:1)

您问题的简单答案是,使用Culture使用GregorianCalendar作为格式提供者,就像SonerGönül在his answer末尾所建议的那样。

但是,尝试调整DateTime,以使其Year属性显示2558而不是2015

即使DateTime没有使用特定日历作为存储日期,它的属性也总是根据格里高利历提供日期和时间部分。这导致以下:

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

但:

buddhistCalendar.ToDateTime(2558,5,22,0,0,0,0) == new DateTime(2015,5,22,0,0,0,0)

如果您想获得特定日历的给定DateTime的年份或任何其他值,请使用calendar.GetYear(dateTime)等日历的相应方法表单。

否则你会得到

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

但:

buddhistCalendar.GetYear(new DateTime(2558,5,22,0,0,0,0)) == 3101

答案 3 :(得分: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");

警告:如果您的本地时间不在泰国,则您的年份将是卑诗省-543 Let's Try it in the fiddle