DateTimeOffset.FromUnixTimeMilliseconds() Method in C# - GeeksforGeeks

28 Feb, 2019
DateTimeOffset.FromUnixTimeMilliseconds(Int64) Method is used to convert a Unix time expressed as the number of milliseconds which have elapsed since 1970-01-01T00:00:00Z to a DateTimeOffset value.
Syntax: public static DateTimeOffset FromUnixTimeMilliseconds (long milliseconds);
Here, it takes a Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative.
Return Value: This method returns a date and time value that represents the same moment in time as the Unix time.
Exception: This method will give ArgumentOutOfRangeException if milliseconds is less than -62,135,596,800,000 or milliseconds is greater than 253,402,300,799,999.
Below programs illustrate the use of DateTimeOffset.FromUnixTimeMilliseconds(Int64) Method:
Example 1:
// C# program to demonstrate the// DateTimeOffset.FromUnixTimeMilliseconds(Int64)// Methodusing System;using System.Globalization;  class GFG {      // Main Method    public static void Main()    {        try {              // Converts a Unix time expressed as             // the number of milliseconds that            // have elapsed since 1970-01-01T00:00:00Z             // to a DateTimeOffset value.            // instance using AddYears() method            DateTimeOffset value =                 DateTimeOffset.FromUnixTimeMilliseconds(100000);              // Display the time            Console.WriteLine("DateTimeOffset is {0}", value);        }          catch (ArgumentOutOfRangeException e)        {            Console.Write("Exception Thrown: ");            Console.Write("{0}", e.GetType(), e.Message);        }    }}
DateTimeOffset is 01/01/1970 00:01:40 +00:00

Example 2: For ArgumentOutOfRangeException
// C# program to demonstrate the// DateTimeOffset.FromUnixTimeMilliseconds(Int64)// Methodusing System;using System.Globalization;  class GFG {      // Main Method    public static void Main()    {        try {              // Converts a Unix time expressed as the            // number of milliseconds that have elapsed             // since 1970-01-01T00:00:00Z to a DateTimeOffset            // value instance using AddYears() method            DateTimeOffset value =                  DateTimeOffset.FromUnixTimeMilliseconds(253502300799999);              // Display the time            Console.WriteLine("DateTimeOffset is {0}", value);        }          catch (ArgumentOutOfRangeException e)        {            Console.Write("Exception Thrown: ");            Console.Write("{0}", e.GetType(), e.Message);        }    }}
Exception Thrown: System.ArgumentOutOfRangeException

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimemilliseconds?view=netframework-4.7.2
CSharp-DateTimeOffset-Struct
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | List Class
C# | Inheritance
Partial Classes in C#
Convert String to Character Array in C#
Lambda Expressions in C#
Difference between Hashtable and Dictionary in C#