DateTimeOffset.FromUnixTimeSeconds() Method in C#

DateTimeOffset.FromUnixTimeSeconds(Int64) Method is used to convert a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a DateTimeOffset value.

Syntax: public static DateTimeOffset FromUnixTimeSeconds (long seconds);
Here, it takes a Unix time, expressed as the number of seconds 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 s date and time value that represents the same moment in time as the Unix time.

Exception: This method will give ArgumentOutOfRangeException if seconds is less than -62,135,596,800 or seconds is greater than 253,402,300,799.

Below programs illustrate the use of DateTimeOffset.FromUnixTimeSeconds(Int64) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

DateTimeOffset is 01/01/1970 02:46:40 +00:00

Example 2: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Exception Thrown: System.ArgumentOutOfRangeException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.