3

I am trying to convert a specific Persian date to Gregorian with no success. I have tried below code but I get compiler error saying:

DateTime does not contain a constructor that takes 4 arguments.

using System.Globalization;

DateTime dt = new DateTime(year, month, day, new PersianCalendar());

I have also tried below way but I get the same Persian date (obj in below code) that I have passed into ConvertToGregorian function and not the Gregorian date:

public static DateTime ConvertToGregorian(this DateTime obj)
    {
        GregorianCalendar gregorian = new GregorianCalendar();
        int y = gregorian.GetYear(obj);
        int m = gregorian.GetMonth(obj);
        int d = gregorian.GetDayOfMonth(obj);
        DateTime gregorianDate = new DateTime(y, m, d);
        var result = gregorianDate.ToString(CultureInfo.InvariantCulture);
        DateTime dt = Convert.ToDateTime(result);
        return dt;
    }

Please note that my CultureInfo.InvariantCulture is English US.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
azad
  • 65
  • 1
  • 6
  • 2
    `CultureInfo.InvariantCulture is English US` --- What? – Nikhil Agrawal Jan 19 '16 at 10:31
  • @NikhilAgrawal that's not entirely wrong: “The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region." (https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx) – Albireo Jan 19 '16 at 10:35
  • 1
    That your code work for me: `new DateTime(year, month, day, new PersianCalendar())`. And [MSDN](https://msdn.microsoft.com/library/sk65c9c1) says that given constructor here since .NET 1.1. – user4003407 Jan 19 '16 at 10:36
  • @ PetSerAl : will you please let me know which VS version you are using? – azad Jan 19 '16 at 10:40
  • That's because `DateTime` is simple (and broken, but for other reasons), and doesn't maintain a reference to the calendar. Have a `DateTime`? It's a Gregorian Date, period. – Clockwork-Muse Jan 19 '16 at 10:45
  • @azad Just now I used `csi.exe` included with Visual Studio Community 2015 Update 1. – user4003407 Jan 19 '16 at 10:47
  • @azad - Phone, UWP or regular .NET? – H H Jan 19 '16 at 10:49
  • I am working on a UWP application while having this problem. – azad Jan 19 '16 at 10:58
  • @azad You possible have to use [this](https://msdn.microsoft.com/library/3c1445e1.aspx) method instead: `new PersianCalendar().ToDateTime(year, month, day, 0, 0, 0, 0)` – user4003407 Jan 19 '16 at 11:01
  • @PetSerAl - new PersianCalendar().ToDateTime(year, month, day, 0, 0, 0, 0) doesn't work either – azad Jan 19 '16 at 11:46
  • @PetSerAl: problem solved with: obj = persian.ToDateTime(year, month, day, 0, 0, 0, 0); Thanks for the hint! – azad Jan 20 '16 at 09:13

1 Answers1

2

As Clockwork-Muse says, DateTime does not maintain a reference to the calendar it was converted from, or should be displayed as, so this information must be maintained outside the DateTime object. Here is an example solution:

using System;
using System.Globalization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Prepare to write the date and time data.
            string FileName = string.Format(@"C:\users\public\documents\{0}.txt", Guid.NewGuid());
            StreamWriter sw = new StreamWriter(FileName);

            //Create a Persian calendar class
            PersianCalendar pc = new PersianCalendar();

            // Create a date using the Persian calendar.
            DateTime wantedDate = pc.ToDateTime(1395, 4, 22, 12, 30, 0, 0);
            sw.WriteLine("Gregorian Calendar:  {0:O} ", wantedDate);
            sw.WriteLine("Persian Calendar:    {0}, {1}/{2}/{3} {4}:{5}:{6}\n",
                          pc.GetDayOfWeek(wantedDate),
                          pc.GetMonth(wantedDate),
                          pc.GetDayOfMonth(wantedDate),
                          pc.GetYear(wantedDate),
                          pc.GetHour(wantedDate),
                          pc.GetMinute(wantedDate),
                          pc.GetSecond(wantedDate));

            sw.Close();
        }
    }
}

The result is:

Gregorian Calendar: 2016-07-12T12:30:00.0000000

Persian Calendar: Tuesday, 4/22/1395 12:30:0

Reading up on the format specification "O", the Gregorian result lacks any time zone indication, meaning that the "Kind" of the DateTime is "Unspecified". Adjustments should be made if the original poster knows, and cares about, what time zone the date is associated with.

Gerard Ashton
  • 560
  • 4
  • 16