DateTimeOffset.FromUnixTimeMilliseconds() Method in C#


The DateTimeOffset.FromUnixTimeMilliseconds() method in C# is used to convert a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a DateTimeOffset value.

Syntax

Following is the syntax −

public static DateTimeOffset FromUnixTimeMilliseconds (long val);

Above, Val are the milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC).

Example

Let us now see an example to implement the DateTimeOffset.FromUnixTimeMilliseconds() method −

using System;
public class Demo {
   public static void Main() {
      DateTimeOffset offset = DateTimeOffset.FromUnixTimeMilliseconds(30000);
      Console.WriteLine("DateTimeOffset = {0} ",offset);
      Console.WriteLine("DateTimeOffset (other format) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ",offset);
   }
}

Output

This will produce the following output −

DateTimeOffset = 1/1/1970 12:00:30 AM +00:00
DateTimeOffset (other format) = 01 January 1970, 12:00:30

Example

Let us now see another example to implement the DateTimeOffset.FromUnixTimeMilliseconds() method −

using System;
public class Demo {
   public static void Main() {
      DateTimeOffset offset = DateTimeOffset.FromUnixTimeMilliseconds(0);
      Console.WriteLine("DateTimeOffset = {0} ",offset);
      Console.WriteLine("DateTimeOffset (other format) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ",offset);
   }
}

Output

This will produce the following output −

DateTimeOffset = 1/1/1970 12:00:00 AM +00:00
DateTimeOffset (other format) = 01 January 1970, 12:00:00

Updated on: 06-Nov-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements