1

DateTime.Now in C# must return '11/30/2019 11:10:12 AM' but it returns '09/09/1398 11:10:12 ق.ظ' in Asp.Net web Applications i have this problem, but in C# console application does not any problem.

DateTime dateTime = DateTime.Now;

enter image description here

yaghob abbasi
  • 96
  • 1
  • 12
  • Can you please post your [MCVE] as a plain text? Imgur was blocked in Turkey, so.. – Soner Gönül Nov 30 '19 at 08:36
  • DateTime dateTime = DateTime.Now; – yaghob abbasi Nov 30 '19 at 08:46
  • Please pay attention that this problem is just for c# web applications but it returns correct date time in Gregorian format. – yaghob abbasi Nov 30 '19 at 08:56
  • 1
    @yaghobabbasi Did you check the linked question? A DateTime doesn't have a culture. Your problem emerges only (and correct me if I'm wrong) when you try to _display_ the DateTime value. Does `DateTime.Now.ToString(CultureInfo.InvariantCulture)` not solve this problem? – 41686d6564 stands w. Palestine Nov 30 '19 at 08:58
  • You could take a look at Nodatime https://nodatime.org/ or nuget package directly https://www.nuget.org/packages/NodaTime. No more time zone issues. – rfcdejong Dec 01 '19 at 21:16

2 Answers2

5

The DateTime type itself is always in the Gregorian calendar, in terms of the values returned by Year, Month and Day.

What you're observing is the result of formatting the DateTime, which converts it in to the default calendar system for the default thread culture. The simplest way to fix this is to use the invariant culture:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        var dateTime = DateTime.Now;
        string formatted = dateTime.ToString(
            "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
        Console.WriteLine(formatted); // 2019-11-30 09:19:43 or similar
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Many thanks, this work for me. is there any way to correct this problem on windows server 2016? – yaghob abbasi Nov 30 '19 at 10:17
  • @yaghobabbasi: That code should work fine on Windows Server 2016. I'm not sure what you're asking. – Jon Skeet Nov 30 '19 at 12:31
  • It does not convert DateTime in a view, I have: value="@(Model.Date.Value.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))" in an input text, But it does not return a gregorian date! also I tried {Model.Date.Value.Year}-... when I hover over Date or Value it shows correct english date but when I hover on year, that is something else. I tried both .Net Framework MVC and MVC core 7 that are the same. is there any solution for this? – mz1378 Aug 25 '23 at 08:16
  • @mz1378: Please create a new question with a [mcve] - it sounds like you've got a problem with how you're obtaining the DateTime to start with. – Jon Skeet Aug 25 '23 at 08:37
0

You could also try this to force it to return DateTime with the standard US culture info.

CultureInfo MyCultureInfo = new CultureInfo("en-US");
DateTime MyNewDate = DateTime.Parse(DateTime.Now.ToString(), MyCultureInfo);
Formula12
  • 305
  • 1
  • 3
  • 14
  • Many thanks for your response but when i run your code i got this error: {"The string was not recognized as a valid DateTime. There is an unknown word starting at index 20."} because DateTime.Now return '09/09/1398 11:10:12 ق.ظ' – yaghob abbasi Nov 30 '19 at 08:35