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"
}
}
}
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
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With