CSharpBeginner CSharpBeginner - 9 months ago 36
C# Question

DateTime display invalid time

I'm using C#.

When I'm trying to get the current time with the follow line:

var time = TimeSpan.FromMilliseconds((dtNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()).TotalMilliseconds);


And to check it with

var date = new DateTime(time.Ticks);


I got invalid year, the year that I'm get is 0048 instead of 2017.

Why its happend?How can i fix it and to know if the
time
contains valid date and if not to fix it? (add the years)

Thanks!

Answer Source

As per the documentation, the ticks parameter is not from 1970, it's from the year 1:

ticks
Type: System.Int64
A date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

(my emphasis)