Home > other >  .NET 6 DateTimeOffset.FromUnixTimeSeconds() is throwing an error
.NET 6 DateTimeOffset.FromUnixTimeSeconds() is throwing an error

Time:02-23

When I run this code in a XUnit test class:

 var x = DateTimeOffset.FromUnixTimeSeconds(1645614860415);
 Assert.IsType<DateTimeOffset>(x);

I have an exception:

System.ArgumentOutOfRangeException
  HResult=0x80131502
  Message=Valid values are between -62135596800 and 253402300799, inclusive. Arg_ParamName_Name
  Source=System.Private.CoreLib
  StackTrace:
   at System.DateTimeOffset.FromUnixTimeSeconds(Int64 seconds)
   at Test_Lib.Unit.....

I have checked the value 1645614860415 and it is a valid number. Why is this happening?

CodePudding user response:

I have checked the value 1645614860415 and it is a valid number.

Not as a number of seconds since the Unix epoch, it's not - at least not for DateTimeOffset.

1645614860415 seconds is over 50,000 years... whereas 1645614860415 milliseconds since the Unix epoch is today.

So basically you want DateTimeOffset.FromUnixTimeMilliseconds.

One good site for testing this is enter image description here Maybe you wanted to do with Miliseconds so you have result with that. enter image description here

  •  Tags:  
  • c#
  • Related