Edit

Share via


DateTimeOffset Constructors

Definition

Initializes a new instance of the DateTimeOffset structure.

Overloads

DateTimeOffset(DateTime)

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

DateTimeOffset(DateTime, TimeSpan)

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

DateTimeOffset(Int64, TimeSpan)

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

DateTimeOffset(DateOnly, TimeOnly, TimeSpan)

Initializes a new instance of the DateTimeOffset structure using the specified date, time, and offset.

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)

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)

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(Int32, 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, microsecond and offset.

DateTimeOffset(Int32, 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, microsecond and offset.

DateTimeOffset(DateTime)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(DateTime dateTime);

Parameters

dateTime
DateTime

A date and time.

Exceptions

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

-or-

The UTC date and time that results from applying the offset is later than DateTimeOffset.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.

C#
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

Remarks

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

See also

Applies to

.NET 9 and other versions
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

DateTimeOffset(DateTime, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(DateTime dateTime, TimeSpan offset);

Parameters

dateTime
DateTime

A date and time.

offset
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 DateTimeOffset.MinValue or greater than DateTimeOffset.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.

C#
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

Remarks

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

See also

Applies to

.NET 9 and other versions
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

DateTimeOffset(Int64, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(long ticks, TimeSpan offset);

Parameters

ticks
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

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

Exceptions

offset is not specified in whole minutes.

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

-or-

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

-or-

offset is 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.

C#
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

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:

C#
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

See also

Applies to

.NET 9 and other versions
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

DateTimeOffset(DateOnly, TimeOnly, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Initializes a new instance of the DateTimeOffset structure using the specified date, time, and offset.

C#
public DateTimeOffset(DateOnly date, TimeOnly time, TimeSpan offset);

Parameters

date
DateOnly

The date part.

time
TimeOnly

The time part.

offset
TimeSpan

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

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

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

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, TimeSpan offset);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

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

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

offset
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 DateTimeOffset.MinValue or later than DateTimeOffset.MaxValue.

Examples

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

C#
   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

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

Applies to

.NET 9 and other versions
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

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

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

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

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

offset
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 DateTimeOffset.MinValue or later than DateTimeOffset.MaxValue.

Examples

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

C#
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

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

Applies to

.NET 9 and other versions
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

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

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, TimeSpan offset);

Parameters

year
Int32

The year.

month
Int32

The month (1 through 12).

day
Int32

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

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

calendar
Calendar

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

offset
TimeSpan

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

Exceptions

offset does not represent whole minutes.

calendar cannot be null.

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 DateTimeOffset.MinValue or later than DateTimeOffset.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.

C#
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();

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. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 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 2.0, 2.1

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

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, TimeSpan offset);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

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

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

microsecond
Int32

The microseconds (0 through 999).

offset
TimeSpan

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

Exceptions

offset does not represent whole minutes.

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

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

-or-

hour is less than 0 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-

microsecond is less than 0 or greater than 999.

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, Int32, Calendar, TimeSpan) constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

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

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

C#
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar, TimeSpan offset);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

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

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

microsecond
Int32

The microseconds (0 through 999).

calendar
Calendar

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

offset
TimeSpan

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

Exceptions

offset does not represent whole minutes.

year is not in the range supported by calendar.

-or-

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

-or-

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

-or-

hour is less than 0 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-

microsecond 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.

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, Int32, Calendar, TimeSpan) constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9