The Universal Windows Platform (UWP) DateTime doesn't seem to support this constructor of DateTime ( link ).
DateTime Constructor (Int32, Int32, Int32, Calendar)
A core functionality of the app I am writing is to convert DateTimes between different Calendar formats (eg gregorian, hijri, hebrew), and previously I relied on that constructor for performing the conversion.
Any idea how I could perform date conversion on a Universal App now that this ctor is missing? eg convert a Hijri/Hebrew Date to Greogiran?
-Tariq
If you want to use .NET classes, these is a ToDateTime method in Calendar class to convert a date in that calendar to a DateTime (Gregorian):
int persianYear = 1394;
int persianMonth = 8;
int persianDay = 10;
var persianCalendar = new System.Globalization.PersianCalendar();
DateTime gregDateTime = persianCalendar.ToDateTime(persianYear, persianMonth, persianDay, 0, 0, 0, 0);
Debug.WriteLine(
string.Format(
"{0}/{1}/{2} in persian is {3}/{4}/{5} in gregorian",
persianYear,
persianMonth,
persianDay,
gregDateTime.Year,
gregDateTime.Month,
gregDateTime.Day
));
output:
1394/8/10 in persian is 2015/11/1 in gregorian
There are also GetYear , GetMonth and similar methods to convert back.
But if you want to go with Windows Runtime Classes (Recommended), the Calendar class is available with a ChangeCalendarSystem method to do the conversion. You can use Year, Month and Day properties to get and set date components.
Here is a sample convert method that converts date components from a calendar system to another:
private void ConvertDate(
string from,
int year,
int month,
int day,
string to,
out int convertedYear,
out int convertedMonth,
out int convertedDay) {
Calendar calendar = new Calendar();
calendar.ChangeCalendarSystem(from);
calendar.Year = year;
calendar.Month = month;
calendar.Day = day;
calendar.ChangeCalendarSystem(to);
convertedYear = calendar.Year;
convertedMonth = calendar.Month;
convertedDay = calendar.Day;
}
An example to use the method to convert Persian to Hijri and Hijri to Gregorian:
int persianYear = 1394;
int persianMonth = 8;
int persianDay = 10;
int hijriYear;
int hijriMonth;
int hijriDay;
ConvertDate(
CalendarIdentifiers.Persian,
persianYear,
persianMonth,
persianDay,
CalendarIdentifiers.Hijri,
out hijriYear,
out hijriMonth,
out hijriDay);
Debug.WriteLine(
string.Format(
"{0}/{1}/{2} in Persian is {3}/{4}/{5} in Hijri",
persianYear,
persianMonth,
persianDay,
hijriYear,
hijriMonth,
hijriDay
));
int gregYear;
int gregMonth;
int gregDay;
ConvertDate(
CalendarIdentifiers.Hijri,
hijriYear,
hijriMonth,
hijriDay,
CalendarIdentifiers.Gregorian,
out gregYear,
out gregMonth,
out gregDay);
Debug.WriteLine(
string.Format(
"{0}/{1}/{2} in Hijri is {3}-{4}-{5} in Gregorian",
hijriYear,
hijriMonth,
hijriDay,
gregYear,
gregMonth,
gregDay
));
Output:
1394/8/10 in Persian is 1437/1/19 in Hijri
1437/1/19 in Hijri is 2015/11/1 in Gregorian
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.