DateTimeOffset DateTimeOffset DateTimeOffset DateTimeOffset Constructors

Definition

Overloads

DateTimeOffset(DateTime) DateTimeOffset(DateTime) DateTimeOffset(DateTime)

Initializes a new instance of the DateTimeOffset structure using the specified DateTime value.

DateTimeOffset(DateTime, TimeSpan) DateTimeOffset(DateTime, TimeSpan) DateTimeOffset(DateTime, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified DateTime value and offset.

DateTimeOffset(Int64, TimeSpan) DateTimeOffset(Int64, TimeSpan) DateTimeOffset(Int64, TimeSpan) DateTimeOffset(Int64, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified number of ticks and offset.

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified year, month, day, hour, minute, second, and offset.

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified year, month, day, hour, minute, second, millisecond, and offset.

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified year, month, day, hour, minute, second, millisecond, and offset of a specified calendar.

DateTimeOffset(DateTime) DateTimeOffset(DateTime) DateTimeOffset(DateTime)

Initializes a new instance of the DateTimeOffset structure using the specified DateTime value.

public:
 DateTimeOffset(DateTime dateTime);
public DateTimeOffset (DateTime dateTime);
new DateTimeOffset : DateTime -> DateTimeOffset

Parameters

dateTime
DateTime DateTime DateTime DateTime

A date and time.

Exceptions

The Coordinated Universal Time (UTC) date and time that results from applying the offset is earlier than MinValue.

-or-

The UTC date and time that results from applying the offset is later than MaxValue.

Examples

The following example illustrates how the value of the DateTime.Kind property of the dateTime parameter affects the date and time value that is returned by this constructor.

DateTime localNow = DateTime.Now;
DateTimeOffset localOffset = new DateTimeOffset(localNow);
Console.WriteLine(localOffset.ToString());

DateTime utcNow = DateTime.UtcNow;
DateTimeOffset utcOffset = new DateTimeOffset(utcNow);
Console.WriteLine(utcOffset.ToString());

DateTime unspecifiedNow = DateTime.SpecifyKind(DateTime.Now, 
                               DateTimeKind.Unspecified);
DateTimeOffset unspecifiedOffset = new DateTimeOffset(unspecifiedNow);
Console.WriteLine(unspecifiedOffset.ToString());
//
// The code produces the following output if run on Feb. 23, 2007, on
// a system 8 hours earlier than UTC:
//   2/23/2007 4:21:58 PM -08:00
//   2/24/2007 12:21:58 AM +00:00
//   2/23/2007 4:21:58 PM -08:00      
Dim localNow As Date = Date.Now
Dim localOffset As New DateTimeOffset(localNow)
Console.WriteLine(localOffset.ToString())

Dim utcNow As Date = Date.UtcNow
Dim utcOffset As New DateTimeOffset(utcNow)
Console.WriteLine(utcOffset.ToString())

Dim unspecifiedNow As Date = Date.SpecifyKind(Date.Now, _
                                  DateTimeKind.Unspecified)
Dim unspecifiedOffset As New DateTimeOffset(unspecifiedNow)
Console.WriteLine(unspecifiedOffset.ToString())
'
' The code produces the following output if run on Feb. 23, 2007, on
' a system 8 hours earlier than UTC:
'    2/23/2007 4:21:58 PM -08:00
'    2/24/2007 12:21:58 AM +00:00
'    2/23/2007 4:21:58 PM -08:00      

Remarks

This constructor's behavior depends on the value of the DateTime.Kind property of the dateTime parameter:

See also

DateTimeOffset(DateTime, TimeSpan) DateTimeOffset(DateTime, TimeSpan) DateTimeOffset(DateTime, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified DateTime value and offset.

public:
 DateTimeOffset(DateTime dateTime, TimeSpan offset);
public DateTimeOffset (DateTime dateTime, TimeSpan offset);
new DateTimeOffset : DateTime * TimeSpan -> DateTimeOffset

Parameters

dateTime
DateTime DateTime DateTime DateTime

A date and time.

offset
TimeSpan TimeSpan TimeSpan TimeSpan

The time's offset from Coordinated Universal Time (UTC).

Exceptions

dateTime.Kind equals Utc and offset does not equal zero.

-or-

dateTime.Kind equals Local and offset does not equal the offset of the system's local time zone.

-or-

offset is not specified in whole minutes.

offset is less than -14 hours or greater than 14 hours.

-or-

UtcDateTime is less than MinValue or greater than MaxValue.

Examples

The following example shows how to initialize a DateTimeOffset object with a date and time and the offset of the local time zone when that time zone is not known in advance.

DateTime localTime = new DateTime(2007, 07, 12, 06, 32, 00); 
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime, 
                         TimeZoneInfo.Local.GetUtcOffset(localTime));
Console.WriteLine(dateAndOffset);
// The code produces the following output:
//    7/12/2007 6:32:00 AM -07:00
Dim localTime As Date = #07/12/2007 6:32AM#
Dim dateAndOffset As New DateTimeOffset(localTime, _
                         TimeZoneInfo.Local.GetUtcOffset(localTime))
Console.WriteLine(dateAndOffset)
' The code produces the following output:
'    7/12/2007 6:32:00 AM -07:00

Remarks

This constructor's behavior depends in part on the value of the Kind property of the dateTime parameter:

See also

DateTimeOffset(Int64, TimeSpan) DateTimeOffset(Int64, TimeSpan) DateTimeOffset(Int64, TimeSpan) DateTimeOffset(Int64, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified number of ticks and offset.

public:
 DateTimeOffset(long ticks, TimeSpan offset);
public DateTimeOffset (long ticks, TimeSpan offset);
new DateTimeOffset : int64 * TimeSpan -> DateTimeOffset
Public Sub New (ticks As Long, offset As TimeSpan)

Parameters

ticks
Int64 Int64 Int64 Int64

A date and time expressed as the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight on January 1, 0001.

offset
TimeSpan TimeSpan TimeSpan TimeSpan

The time's offset from Coordinated Universal Time (UTC).

Exceptions

offset is not specified in whole minutes.

The UtcDateTime property is earlier than MinValue or later than MaxValue.

-or-

ticks is less than DateTimeOffset.MinValue.Ticks or greater than DateTimeOffset.MaxValue.Ticks.

-or-

Offset s less than -14 hours or greater than 14 hours.

Examples

The following example initializes a DateTimeOffset object by using the number of ticks in an arbitrary date (in this case, July 16, 2007, at 1:32 PM) with an offset of -5.

DateTime dateWithoutOffset = new DateTime(2007, 7, 16, 13, 32, 00);
DateTimeOffset timeFromTicks = new DateTimeOffset(dateWithoutOffset.Ticks, 
                               new TimeSpan(-5, 0, 0));
Console.WriteLine(timeFromTicks.ToString());
// The code produces the following output:
//    7/16/2007 1:32:00 PM -05:00
Dim dateWithoutOffset As Date = #07/16/2007 1:32PM#
Dim timeFromTicks As New DateTimeOffset(datewithoutOffset.Ticks, _
                         New TimeSpan(-5, 0, 0))
Console.WriteLine(timeFromTicks.ToString())
' The code produces the following output:
'    7/16/2007 1:32:00 PM -05:00

Remarks

Ordinarily, trying to call the DateTimeOffset constructor to instantiate a DateTimeOffset value with a local time and an offset other than that of the local time zone throws an ArgumentException. You can use this overload of the DateTimeOffset constructor to work around this limitation. The following example uses the local time's number of ticks to instantiate a DateTimeOffset value whose offset does not necessarily represent that of the local time:

DateTime localTime = DateTime.Now;
DateTimeOffset nonLocalDateWithOffset = new DateTimeOffset(localTime.Ticks, 
                                  new TimeSpan(2, 0, 0));
Console.WriteLine(nonLocalDateWithOffset); 
//
// The code produces the following output if run on Feb. 23, 2007:
//    2/23/2007 4:37:50 PM +02:00
Dim localTime As Date = Date.Now
Dim nonLocalDateWithOffset As New DateTimeOffset(localTime.Ticks, _
                                  New TimeSpan(2, 0, 0))
Console.WriteLine(nonLocalDateWithOffset)                                        
'
' The code produces the following output if run on Feb. 23, 2007:
'    2/23/2007 4:37:50 PM +02:00
See also

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified year, month, day, hour, minute, second, and offset.

public:
 DateTimeOffset(int year, int month, int day, int hour, int minute, int second, TimeSpan offset);
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, TimeSpan offset);
new DateTimeOffset : int * int * int * int * int * int * TimeSpan -> DateTimeOffset
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, offset As TimeSpan)

Parameters

year
Int32 Int32 Int32 Int32

The year (1 through 9999).

month
Int32 Int32 Int32 Int32

The month (1 through 12).

day
Int32 Int32 Int32 Int32

The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

The seconds (0 through 59).

offset
TimeSpan TimeSpan TimeSpan TimeSpan

The time's offset from Coordinated Universal Time (UTC).

Exceptions

offset does not represent whole minutes.

year is less than one or greater than 9999.

-or-

month is less than one or greater than 12.

-or-

day is less than one or greater than the number of days in month.

-or-

hour is less than zero or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

offset is less than -14 hours or greater than 14 hours.

-or-

The UtcDateTime property is earlier than MinValue or later than MaxValue.

Examples

The following example instantiates a DateTimeOffset object by using the DateTimeOffset.DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) constructor overload.

   DateTime specificDate = new DateTime(2008, 5, 1, 06, 32, 00); 
   DateTimeOffset offsetDate = new DateTimeOffset(specificDate.Year, 
                                   specificDate.Month, 
                                   specificDate.Day, 
                                   specificDate.Hour, 
                                   specificDate.Minute, 
                                   specificDate.Second, 
                                   new TimeSpan(-5, 0, 0));
   Console.WriteLine("Current time: {0}", offsetDate);
   Console.WriteLine("Corresponding UTC time: {0}", offsetDate.UtcDateTime);                                              
// The code produces the following output:
//    Current time: 5/1/2008 6:32:00 AM -05:00
//    Corresponding UTC time: 5/1/2008 11:32:00 AM      
   Dim specificDate As Date = #5/1/2008 6:32AM#
   Dim offsetDate As New DateTimeOffset(specificDate.Year, _
                                        specificDate.Month, _
                                        specificDate.Day, _
                                        specificDate.Hour, _
                                        specificDate.Minute, _
                                        specificDate.Second, _
                                        New TimeSpan(-5, 0, 0))
   Console.WriteLine("Current time: {0}", offsetDate)
   Console.WriteLine("Corresponding UTC time: {0}", offsetDate.UtcDateTime)                                              
' The code produces the following output:
'    Current time: 5/1/2008 6:32:00 AM -05:00
'    Corresponding UTC time: 5/1/2008 11:32:00 AM      

Remarks

This constructor interprets year, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTimeOffset value by using the year, month, and day in another calendar, call the DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) constructor.

See also

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified year, month, day, hour, minute, second, millisecond, and offset.

public:
 DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset);
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset);
new DateTimeOffset : int * int * int * int * int * int * int * TimeSpan -> DateTimeOffset
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, offset As TimeSpan)

Parameters

year
Int32 Int32 Int32 Int32

The year (1 through 9999).

month
Int32 Int32 Int32 Int32

The month (1 through 12).

day
Int32 Int32 Int32 Int32

The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

The seconds (0 through 59).

millisecond
Int32 Int32 Int32 Int32

The milliseconds (0 through 999).

offset
TimeSpan TimeSpan TimeSpan TimeSpan

The time's offset from Coordinated Universal Time (UTC).

Exceptions

offset does not represent whole minutes.

year is less than one or greater than 9999.

-or-

month is less than one or greater than 12.

-or-

day is less than one or greater than the number of days in month.

-or-

hour is less than zero or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

-or-

offset is less than -14 or greater than 14.

-or-

The UtcDateTime property is earlier than MinValue or later than MaxValue.

Examples

The following example instantiates a DateTimeOffset object by using the DateTimeOffset.DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) constructor overload.

string fmt = "dd MMM yyyy HH:mm:ss";
DateTime thisDate = new DateTime(2007, 06, 12, 19, 00, 14, 16);
DateTimeOffset offsetDate = new DateTimeOffset(thisDate.Year, 
                                               thisDate.Month, 
                                               thisDate.Day, 
                                               thisDate.Hour,
                                               thisDate.Minute,
                                               thisDate.Second,
                                               thisDate.Millisecond, 
                                               new TimeSpan(2, 0, 0));  
Console.WriteLine("Current time: {0}:{1}", offsetDate.ToString(fmt), offsetDate.Millisecond);
// The code produces the following output:
//    Current time: 12 Jun 2007 19:00:14:16      
Dim fmt As String = "dd MMM yyyy HH:mm:ss"
Dim thisDate As DateTime = New Date(2007, 06, 12, 19, 00, 14, 16)
Dim offsetDate As New DateTimeOffset(thisDate.Year, _
                                     thisDate.Month, _
                                     thisDate.Day, _
                                     thisDate.Hour, _
                                     thisDate.Minute, _
                                     thisDate.Second, _
                                     thisDate.Millisecond, _ 
                                     New TimeSpan(2, 0, 0))  
Console.WriteLine("Current time: {0}:{1}", offsetDate.ToString(fmt), _ 
                                           offsetDate.Millisecond)
' The code produces the following output:
'    Current time: 12 Jun 2007 19:00:14:16      

Remarks

This constructor interprets year, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTimeOffset value by using the year, month, and day in another calendar, call the DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) constructor.

See also

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified year, month, day, hour, minute, second, millisecond, and offset of a specified calendar.

public:
 DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, System::Globalization::Calendar ^ calendar, TimeSpan offset);
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, TimeSpan offset);
new DateTimeOffset : int * int * int * int * int * int * int * System.Globalization.Calendar * TimeSpan -> DateTimeOffset

Parameters

year
Int32 Int32 Int32 Int32

The year.

month
Int32 Int32 Int32 Int32

The month (1 through 12).

day
Int32 Int32 Int32 Int32

The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

The seconds (0 through 59).

millisecond
Int32 Int32 Int32 Int32

The milliseconds (0 through 999).

calendar
Calendar Calendar Calendar Calendar

The calendar that is used to interpret year, month, and day.

offset
TimeSpan TimeSpan TimeSpan TimeSpan

The time's offset from Coordinated Universal Time (UTC).

Exceptions

offset does not represent whole minutes.

year is less than the calendar parameter's MinSupportedDateTime.Year or greater than MaxSupportedDateTime.Year.

-or-

month is either less than or greater than the number of months in year in the calendar.

-or-

day is less than one or greater than the number of days in month.

-or-

hour is less than zero or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

-or-

offset is less than -14 hours or greater than 14 hours.

-or-

The year, month, and day parameters cannot be represented as a date and time value.

-or-

The UtcDateTime property is earlier than MinValue or later than MaxValue.

Examples

The following example uses instances of both the HebrewCalendar class and the HijriCalendar class to instantiate a DateTimeOffset value. That date is then displayed to the console using the respective calendars and the Gregorian calendar.

CultureInfo fmt;
int year; 
Calendar cal;
DateTimeOffset dateInCal;

// Instantiate DateTimeOffset with Hebrew calendar
year = 5770;
cal = new HebrewCalendar();
fmt = new CultureInfo("he-IL");
fmt.DateTimeFormat.Calendar = cal;      
dateInCal = new DateTimeOffset(year, 7, 12, 
                               15, 30, 0, 0, 
                               cal, 
                               new TimeSpan(2, 0, 0));
// Display the date in the Hebrew calendar
Console.WriteLine("Date in Hebrew Calendar: {0:g}", 
                   dateInCal.ToString(fmt));
// Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);
Console.WriteLine();

// Instantiate DateTimeOffset with Hijri calendar
year = 1431;
cal = new HijriCalendar();
fmt = new CultureInfo("ar-SA");
fmt.DateTimeFormat.Calendar = cal;
dateInCal = new DateTimeOffset(year, 7, 12, 
                               15, 30, 0, 0, 
                               cal, 
                               new TimeSpan(2, 0, 0));
// Display the date in the Hijri calendar
Console.WriteLine("Date in Hijri Calendar: {0:g}", 
                   dateInCal.ToString(fmt));
// Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);
Console.WriteLine();
Dim fmt As CultureInfo
Dim year As Integer
Dim cal As Calendar
Dim dateInCal As DateTimeOffset

' Instantiate DateTimeOffset with Hebrew calendar
year = 5770
cal = New HebrewCalendar()
fmt = New CultureInfo("he-IL")
fmt.DateTimeFormat.Calendar = cal      
dateInCal = New DateTimeOffset(year, 7, 12, _
                               15, 30, 0, 0, _
                               cal, _
                               New TimeSpan(2, 0, 0))
' Display the date in the Hebrew calendar
Console.WriteLine("Date in Hebrew Calendar: {0:g}", _
                   dateInCal.ToString(fmt))
' Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal)
Console.WriteLine()

' Instantiate DateTimeOffset with Hijri calendar
year = 1431
cal = New HijriCalendar()
fmt = New CultureInfo("ar-SA")
fmt.DateTimeFormat.Calendar = cal
dateInCal = New DateTimeOffset(year, 7, 12, _
                               15, 30, 0, 0, _
                               cal, _
                               New TimeSpan(2, 0, 0))
' Display the date in the Hijri calendar
Console.WriteLine("Date in Hijri Calendar: {0:g}", _
                   dateInCal.ToString(fmt))
' Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal)
Console.WriteLine()

Remarks

The year, month, day, hour, minute, second, and millisecond parameters all reflect values expressed in the calendar specified by the calendar parameter. An exception is thrown if these values form a date and time that cannot be expressed by using this calendar.

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. See Handling a new era in the Japanese calendar in .NET for more information and to determine whether your applications are affected. See Prepare your application for the Japanese era change for information on testing your applications on Windows systems to ensure their readiness for the era change. See Working with eras for features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras.

See also

Applies to