Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c# - Convert between calendars

How to convert between calendars? Here is what I have:

UmAlQuraCalendar hijri = new UmAlQuraCalendar();
GregorianCalendar cal = new GregorianCalendar();

DateTime hijriDate = new DateTime(1434, 11, 23, hijri);
DateTime gregorianDate = ...; //

I need a gregorianDate that corresponds to the hijriDate.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A DateTime can accept input in its constructor with an alternative calendar, but internally it is always stored using the Gregorian equivalent. So you already have what you are looking for.

Calendar umAlQura = new UmAlQuraCalendar();
DateTime dt = new DateTime(1434, 11, 23, umAlQura);

// As a string, it will format with whatever the calendar for the culture is.
Debug.WriteLine(dt.ToString("d", CultureInfo.InvariantCulture)); // 09/29/2013
Debug.WriteLine(dt.ToString("d", new CultureInfo("ar-SA")));     // 23/11/34

// But the individual integer properties are always Gregorian
Debug.WriteLine(dt.Year);  // 2013
Debug.WriteLine(dt.Month); // 9
Debug.WriteLine(dt.Day);   // 29

Going the other direction, you have to get the parts using the methods on the calendar object.

DateTime dt = new DateTime(2013, 9, 29);  // Gregorian

Calendar umAlQura = new UmAlQuraCalendar();

Debug.WriteLine(umAlQura.GetYear(dt));       // 1434
Debug.WriteLine(umAlQura.GetMonth(dt));      // 11
Debug.WriteLine(umAlQura.GetDayOfMonth(dt)); // 23

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...