Convert between calendars
Asked Answered
I

3

13

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.

Inheritrix answered 29/9, 2013 at 8:0 Comment(1)
This might be helpful: https://mcmap.net/q/904133/-converting-calendarsWhensoever
D
8

It seems that the Date saved in DateTime is always in the current calendar. So if the current calendar is Gregorian hijriDate is already in Gregorian.

var hijriDate = new DateTime(1434, 11, 23, hijri);
//Console writeline will show 2013-09-29 00:00:00

If your current calendar is UmAlQuraCalendar you should be able to extract a Gregorian date using:

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

var hijriDate = new DateTime(1434, 11, 23, hijri);
var y = cal.GetYear(hijriDate), 
var m = cal.GetMonth(hijriDate), 
var d = cal.GetDayOfMonth(hijriDate)
Dieball answered 29/9, 2013 at 8:18 Comment(2)
Actually, DateTime is always Gregorian internally, so this is slightly redundant.Nourishing
Working Fine. Thanks :)Ixtle
N
19

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
Nourishing answered 29/9, 2013 at 20:13 Comment(0)
D
8

It seems that the Date saved in DateTime is always in the current calendar. So if the current calendar is Gregorian hijriDate is already in Gregorian.

var hijriDate = new DateTime(1434, 11, 23, hijri);
//Console writeline will show 2013-09-29 00:00:00

If your current calendar is UmAlQuraCalendar you should be able to extract a Gregorian date using:

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

var hijriDate = new DateTime(1434, 11, 23, hijri);
var y = cal.GetYear(hijriDate), 
var m = cal.GetMonth(hijriDate), 
var d = cal.GetDayOfMonth(hijriDate)
Dieball answered 29/9, 2013 at 8:18 Comment(2)
Actually, DateTime is always Gregorian internally, so this is slightly redundant.Nourishing
Working Fine. Thanks :)Ixtle
L
2

As extension method

public static DateTime GregorianToUmAlQura(this DateTime gregorianDate)
{
    Calendar umAlQura = new UmAlQuraCalendar();

    return new DateTime(umAlQura.GetYear(gregorianDate), umAlQura.GetMonth(gregorianDate), umAlQura.GetDayOfMonth(gregorianDate), umAlQura);
}
Levelheaded answered 1/2, 2018 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.