Reputation: 111
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"
}
}
}
Upvotes: 7
Views: 19834
Reputation: 379
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
Upvotes: 0
Reputation: 1628
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
Upvotes: 2
Reputation: 98750
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
Upvotes: 2
Reputation: 1430
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);
Upvotes: 1