泰国2558年至2015年

use*_*277 6 c# datetime cultureinfo date-format

如何将日期时间的佛教格式转换为公历格式(例如,泰国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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Son*_*nül 2

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"));
Run Code Online (Sandbox Code Playgroud)

结果将是;

2558-05-22
Run Code Online (Sandbox Code Playgroud)

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

佛历与公历相差543年。欧洲的2015年是泰国的2558年。

如果您寻找相反的方式,只需使用具有公历日历作为一部分的文化,Calender例如InvariantCulture.

var today = DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture)); // 2015-05-22
Run Code Online (Sandbox Code Playgroud)