CSharpBeginner CSharpBeginner - 1 year ago 72
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
contains valid date and if not to fix it? (add the years)


Answer Source

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

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download