DateTimeOffset.Now Property

Definition

Gets a DateTimeOffset object that is set to the current date and time on the current computer, with the offset set to the local time's offset from Coordinated Universal Time (UTC).

C#
public static DateTimeOffset Now { get; }

Property Value

A DateTimeOffset object whose date and time is the current local time and whose offset is the local time zone's offset from Coordinated Universal Time (UTC).

Examples

The following example uses the Now property to retrieve the current date and time and displays it by using each of the standard date and time format strings supported by the DateTimeOffset type.

C#
using System;

public class Example
{
   public static void Main()
   {
      String[] fmtStrings = { "d", "D", "f", "F", "g", "G", "M",
                              "R", "s", "t", "T", "u", "y" };

      DateTimeOffset value = DateTimeOffset.Now;
      // Display date in default format.
      Console.WriteLine(value);
      Console.WriteLine();

      // Display date using each of the specified formats.
      foreach (var fmtString in fmtStrings)
         Console.WriteLine("{0} --> {1}",
                           fmtString, value.ToString(fmtString));
   }
}
// The example displays output similar to the following:
//    11/19/2012 10:57:11 AM -08:00
//
//    d --> 11/19/2012
//    D --> Monday, November 19, 2012
//    f --> Monday, November 19, 2012 10:57 AM
//    F --> Monday, November 19, 2012 10:57:11 AM
//    g --> 11/19/2012 10:57 AM
//    G --> 11/19/2012 10:57:11 AM
//    M --> November 19
//    R --> Mon, 19 Nov 2012 18:57:11 GMT
//    s --> 2012-11-19T10:57:11
//    t --> 10:57 AM
//    T --> 10:57:11 AM
//    u --> 2012-11-19 18:57:11Z
//    y --> November, 2012

The following example uses the Now and Millisecond properties to determine the resolution of the system clock. It displays the time only when the value of its millisecond component has changed.

C#
DateTimeOffset dto;
int ctr = 0;
int ms = 0;
do {
   dto = DateTimeOffset.Now;
   if (dto.Millisecond != ms)
   {
      ms = dto.Millisecond;
      Console.WriteLine("{0}:{1:d3} ms. {2}",
                        dto.ToString("M/d/yyyy h:mm:ss"),
                        ms, dto.ToString("zzz"));
      ctr++;
   }
} while (ctr < 100);

Remarks

The precision of the current local time's millisecond component depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also