Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`DateTime.Parse` parses the timestamp in which timezone?

Tags:

I am parsing string time in date using DateTime.Parse method. It parses properly and returns me today's date with the entered time. But I am not able to determine the timezone when it takes the date part. Is it considering local date or UTC date?

public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        Console.WriteLine(DateTime.Parse("4:00"));
        //prints 09.05.2019 04:00:00 - here did it take 09.05.2019 from local timezone or is it UTC?
    }
}

On referring official documentation of DateTime.Parse, it is mentioned vaguely as follows:

A string with a time but no date component. The method assumes the current date unless you call the Parse(String, IFormatProvider, DateTimeStyles) overload and include DateTimeStyles.NoCurrentDateDefault in the styles argument, in which case the method assumes a date of January 1, 0001.

I am confused what culture's exactly the date is being picked?

like image 432
Karan Desai Avatar asked May 09 '19 06:05

Karan Desai


1 Answers

Neither, it is Unspecified.

First of all, in .NET Framework, DateTime structure does not keep timezone information. It has Kind property which reflects some information as Utc, Local or Unspecified but they are not perfectly refers a timezone.

In your case, since you don't supply any other parameters, your result DateTime will be Unspecified.

It is stated on the documentation as;

If s contains time zone information, this method returns a DateTime value whose Kind property is DateTimeKind.Local and converts the date and time in s to local time. Otherwise, it performs no time zone conversion and returns a DateTime value whose Kind property is DateTimeKind.Unspecified.

For representation part, when you use DateTime in Console.WriteLine method, it calls WriteLine(object) overload and this calls the current FormatProvider which is came from Thread.CurrentThread.CurrentCulture.

like image 99
Soner Gönül Avatar answered Nov 15 '22 05:11

Soner Gönül