Thai year 2558 to 2015
Asked Answered
C

4

7

How to transform buddhist format of datetime into gregorian format (e.g. for Thailand 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"
        }
    }
}
Centrifugate answered 22/5, 2015 at 9:55 Comment(2)
Sorry, in today is 2558-05-22Centrifugate
-543 years? ...Fernyak
K
2

DateTime.Now generates your local time with GregorianCalender by default. If you wanna generate that time in your ThaiBuddhistCalendar, you can get it's values with an instance of that calender object.

Now you can use this values in DateTime(Int32, Int32, Int32, Int32, Int32, Int32) constructor to generate a DateTime.

After that, you can format your DateTime with a specific format.

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"));

Result will be;

2558-05-22

From http://www.thaiworldview.com/feast/feast.htm

There is a 543 years difference between the Buddhist calendar and the Gregorian calendar. Year 2015 in Europe is year 2558 in Thailand.

If you looking for the opposite way, just use a culture that have Gregorian calender as a Calender part like InvariantCulture.

var today = DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture)); // 2015-05-22
Knotting answered 22/5, 2015 at 10:4 Comment(5)
DateTime is independent from any calendar. A call of DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("th-TH")) results in '2558-05-22'.Lancaster
@Lancaster No. A DateTime is Gregorian Calender by default. Even documentation says; The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar. This DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("th-TH")) generates 2558-05-22 because th-TH culture has ThaiBuddhistCalendar as a DateTimeFormatInfo.Calender property.Salutation
This only means it is valid in that range of the Gregorian Calendar. The date 2015-05-22 (Gregorian Calendar) is the same as 2558-05-22 (Thai Buddist Calendar), it only is another representation. You can verify this by comparing gregorianCalendar.ToDateTime(2015,5,22,0,0,0,0) to buddhistCalendar.ToDateTime(2558,5,22,0,0,0,0).Lancaster
@Lancaster Exactly. I say almost same things in a different way.Salutation
buddhistCalendar.ToDateTime(2558,5,22,0,0,0,0) != new DateTime(2558,5,22,0,0,0,0) but buddhistCalendar.ToDateTime(2558,5,22,0,0,0,0) == new DateTime(2015,5,22,0,0,0,0).Lancaster
L
2

The simple answer to your question is, use a Culture which is using the GregorianCalendar as format provider, just like Soner Gönül suggest at the end of his answer.

However, do not try to adjust the DateTime so that its Year property would show 2558 instead of 2015.

Even when the DateTime does not use a specific calendar for the stored date, it's properties always give the date and time parts based on the gregorian calendar. This leads to following:

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

but:

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

If you want to get the year, or any other value, of a given DateTime for a specific calendar, then use the appropriate method form that calendar like calendar.GetYear(dateTime).

Otherwise you would get

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

but:

buddhistCalendar.GetYear(new DateTime(2558,5,22,0,0,0,0)) == 3101
Lancaster answered 24/5, 2015 at 11:46 Comment(0)
P
1

Take a look at the ThaiBuddhistCalendar class.

In example, if you know thai year, you can do this:

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

If you need get Thai DateTime, you can do this:

ThaiBuddhistCalendar cal = new ThaiBuddhistCalendar();
DateTime now = DateTime.Now;
DateTime thai = new DateTime(cal.GetYear(now), cal.GetMonth(now), now.Day);
Plated answered 22/5, 2015 at 10:7 Comment(0)
Q
0

I suggest a simple way to solve your issue.

//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");

Warning : If your local time is not in Thailand Your Year will be B.C - 543 Let's Try it in the fiddle

Quibbling answered 29/6, 2019 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.