Sdílet prostřednictvím


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub New ( _
    year As Integer, _
    month As Integer, _
    day As Integer, _
    hour As Integer, _
    minute As Integer, _
    second As Integer, _
    millisecond As Integer, _
    calendar As Calendar, _
    offset As TimeSpan _
)
public DateTimeOffset(
    int year,
    int month,
    int day,
    int hour,
    int minute,
    int second,
    int millisecond,
    Calendar calendar,
    TimeSpan offset
)

Parameters

  • day
    Type: System.Int32
    The day (1 through the number of days in month).
  • millisecond
    Type: System.Int32
    The milliseconds (0 through 999).
  • offset
    Type: System.TimeSpan
    The time's offset from Coordinated Universal Time (UTC).

Exceptions

Exception Condition
ArgumentException

offset does not represent whole minutes.

ArgumentNullException

calendar cannot be nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

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.

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.

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.

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
outputBlock.Text &= String.Format("Date in Hebrew Calendar: {0:g}", _
                   dateInCal.ToString(fmt)) & vbCrLf
' Display the date in the Gregorian calendar                         
outputBlock.Text &= String.Format("Date in Gregorian Calendar: {0:g}", dateInCal) & vbCrLf
outputBlock.Text &= vbCrLf

' 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
outputBlock.Text &= String.Format("Date in Hijri Calendar: {0:g}", _
                   dateInCal.ToString(fmt)) & vbCrLf
' Display the date in the Gregorian calendar                         
outputBlock.Text &= String.Format("Date in Gregorian Calendar: {0:g}", dateInCal) & vbCrLf
outputBlock.Text &= vbCrLf
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
outputBlock.Text += String.Format("Date in Hebrew Calendar: {0:g}",
                   dateInCal.ToString(fmt)) + "\n";
// Display the date in the Gregorian calendar                         
outputBlock.Text += String.Format("Date in Gregorian Calendar: {0:g}", dateInCal) + "\n";
outputBlock.Text += "\n";

// 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
outputBlock.Text += String.Format("Date in Hijri Calendar: {0:g}",
                   dateInCal.ToString(fmt)) + "\n";
// Display the date in the Gregorian calendar                         
outputBlock.Text += String.Format("Date in Gregorian Calendar: {0:g}", dateInCal) + "\n";
outputBlock.Text += "\n";

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.