Microsoft datetime




















This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Date Time. Date Property Reference Is this page helpful? Please rate your experience Yes No. Any additional feedback? Month in two digits. If the month number is a single-digit number, it is displayed with a leading zero. The name of the month spelled in full.

This format is supported only for output time. Note: This format is only supported for the output format. Day as a number from 1 to If the day number is a single-digit number, it is displayed without a leading zero. Day in two digits. If the day number is a single-digit number, it is displayed with a leading zero. The abbreviated name of the day of the week in three letters. The full name of the day of the week. For example, Saturday is displayed in full.

Hour as a number from 1 to 12 when using the hour clock. If the hour number is a single-digit number, it is displayed without a leading zero. Hour in two digits using the hour clock. For example, to add the number of seconds represented by a specified number of ticks to the Second component of a DateTime value, you can use the expression dateValue.

You can view the source for the entire set of examples from this article in either Visual Basic or C from the docs repository on GitHub. An alternative to the DateTime structure for working with date and time values in particular time zones is the DateTimeOffset structure. The DateTimeOffset structure stores date and time information in a private DateTime field and the number of minutes by which that date and time differs from UTC in a private Int16 field.

This makes it possible for a DateTimeOffset value to reflect the time in a particular time zone, whereas a DateTime value can unambiguously reflect only UTC and the local time zone's time. You call any of the overloads of the DateTime constructor that specify elements of the date and time value such as the year, month, and day, or the number of ticks. The following code creates a specific date using the DateTime constructor specifying the year, month, day, hour, minute, and second.

You invoke the DateTime structure's implicit parameterless constructor when you want a DateTime initialized to its default value. For details on the implicit parameterless constructor of a value type, see Value Types.

Some compilers also support declaring a DateTime value without explicitly assigning a value to it. Creating a value without an explicit initialization also results in the default value. The following example illustrates the DateTime implicit parameterless constructor in C and Visual Basic, as well as a DateTime declaration without assignment in Visual Basic.

You can assign the DateTime object a date and time value returned by a property or method. The following example assigns the current date and time, the current Coordinated Universal Time UTC date and time, and the current date to three new DateTime variables.

The following examples use the Parse and ParseExact methods to parse a string and convert it to a DateTime value. The second format uses a form supported by the ISO standard for a representing date and time in string format. This standard representation is often used to transfer date information in web services. The TryParse and TryParseExact methods indicate whether a string is a valid representation of a DateTime value and, if it is, performs the conversion.

The following Visual Basic statement initializes a new DateTime value. Internally, all DateTime values are represented as the number of ticks the number of nanosecond intervals that have elapsed since midnight, January 1, The actual DateTime value is independent of the way in which that value appears when displayed. The appearance of a DateTime value is the result of a formatting operation that converts a value to its string representation.

The appearance of date and time values is dependent on culture, international standards, application requirements, and personal preference.

The DateTime structure offers flexibility in formatting date and time values through overloads of ToString. The default DateTime. ToString method returns the string representation of a date and time value using the current culture's short date and long time pattern.

The following example uses the default DateTime. ToString method. It displays the date and time using the short date and long time pattern for the current culture. The en-US culture is the current culture on the computer on which the example was run. You may need to format dates in a specific culture to support web scenarios where the server may be in a different culture from the client.

You specify the culture using the DateTime. ToString IFormatProvider method to create the short date and long time representation in a specific culture.

The following example uses the DateTime. ToString IFormatProvider method to display the date and time using the short date and long time pattern for the fr-FR culture. Other applications may require different string representations of a date. The DateTime.

ToString String method returns the string representation defined by a standard or custom format specifier using the formatting conventions of the current culture. ToString String method to display the full date and time pattern for the en-US culture, the current culture on the computer on which the example was run. Finally, you can specify both the culture and the format using the DateTime. ToString String overload can also be used with a custom format string to specify other formats.

The following example shows how to format a string using the ISO standard format often used for web services. The Iso format does not have a corresponding standard format string.

Parsing converts the string representation of a date and time to a DateTime value. Typically, date and time strings have two different usages in applications:. A date and time takes a variety of forms and reflects the conventions of either the current culture or a specific culture. A date and time is represented in a predefined format. For example, an application serializes a date as "" independently of the culture on which the app is running.

An application may require dates be input in the current culture's short date format. You use the Parse or TryParse method to convert a string from one of the common date and time formats used by a culture to a DateTime value. The following example shows how you can use TryParse to convert date strings in different culture-specific formats to a DateTime value. It changes the current culture to English Great Britain and calls the GetDateTimeFormats method to generate an array of date and time strings.

It then passes each element in the array to the TryParse method. The output from the example shows the parsing method was able to successfully convert each of the culture-specific date and time strings. You use the ParseExact and TryParseExact methods to convert a string that must match a particular format or formats to a DateTime value.

You specify one or more date and time format strings as a parameter to the parsing method. One common use for ParseExact is to convert a string representation from a web service, usually in ISO standard format.

The following code shows the correct format string to use:. If a string cannot be parsed, the Parse and ParseExact methods throw an exception. The TryParse and TryParseExact methods return a Boolean value that indicates whether the conversion succeeded or failed. The parsing operation for date and time strings tends to have a high failure rate, and exception handling is expensive.

Use these methods if strings are input by users or coming from an unknown source. For more information about parsing date and time values, see Parsing Date and Time Strings. Daylight saving time is not applicable to UTC. Local time is relative to a particular time zone. A time zone is associated with a time zone offset. A time zone offset is the displacement of the time zone measured in hours from the UTC origin point.

In addition, local time is optionally affected by daylight saving time, which adds or subtracts a time interval adjustment. Local time is calculated by adding the time zone offset to UTC and adjusting for daylight saving time if necessary. The time zone offset at the UTC origin point is zero. UTC time is suitable for calculations, comparisons, and storing dates and time in files. Local time is appropriate for display in user interfaces of desktop applications. Time zone-aware applications such as many Web applications also need to work with a number of other time zones.

Unspecified , it is unspecified whether the time represented is local time, UTC time, or a time in some other time zone. As an alternative to performing date and time arithmetic on DateTime values to measure elapsed time, you can use the Stopwatch class.

The Ticks property expresses date and time values in units of one ten-millionth of a second. The Millisecond property returns the thousandths of a second in a date and time value. Using repeated calls to the DateTime.

Now property to measure elapsed time is dependent on the system clock. The system clock on Windows 7 and Windows 8 systems has a resolution of approximately 15 milliseconds.

This resolution affects small time intervals less than milliseconds. The following example illustrates the dependence of current date and time values on the resolution of the system clock.

In the example, an outer loop repeats 20 times, and an inner loop serves to delay the outer loop. If the value of the outer loop counter is 10, a call to the Thread. Sleep method introduces a five-millisecond delay. The following example shows the number of milliseconds returned by the DateTime. Milliseconds property changes only after the call to Thread. A calculation using a DateTime structure, such as Add or Subtract , does not modify the value of the structure.

Instead, the calculation returns a new DateTime structure whose value is the result of the calculation. Conversion operations between time zones such as between UTC and local time, or between one time zone and another take daylight saving time into account, but arithmetic and comparison operations do not.

The DateTime structure itself offers limited support for converting from one time zone to another. However, a full set of time zone conversion methods is available in the TimeZoneInfo class. You convert the time in any one of the world's time zones to the time in any other time zone using these methods.

Calculations and comparisons of DateTime objects are meaningful only if the objects represent times in the same time zone. You can use a TimeZoneInfo object to represent a DateTime value's time zone, although the two are loosely coupled. A DateTime object does not have a property that returns an object that represents that date and time value's time zone. In a time zone-aware application, you must rely on some external mechanism to determine the time zone in which a DateTime object was created.

You could use a structure that wraps both the DateTime value and the TimeZoneInfo object that represents the DateTime value's time zone. Each DateTime member implicitly uses the Gregorian calendar to perform its operation. Exceptions are methods that implicitly specify a calendar. These include constructors that specify a calendar, and methods with a parameter derived from IFormatProvider , such as System. Operations by members of the DateTime type take into account details such as leap years and the number of days in a month.

They are:. Each culture uses a default calendar defined by its read-only CultureInfo. Calendar property. Each culture may support one or more calendars defined by its read-only CultureInfo. OptionalCalendars property. It must be one of the calendars found in the CultureInfo. OptionalCalendars array.

A culture's current calendar is used in all formatting operations for that culture. For example, the default calendar of the Thai Buddhist culture is the Thai Buddhist Era calendar, which is represented by the ThaiBuddhistCalendar class.

When a CultureInfo object that represents the Thai Buddhist culture is used in a date and time formatting operation, the Thai Buddhist Era calendar is used by default. Calendar property is changed, as the following example shows:.

A culture's current calendar is also used in all parsing operations for that culture, as the following example shows. You instantiate a DateTime value using the date and time elements number of the year, month, and day of a specific calendar by calling a DateTime constructor that includes a calendar parameter and passing it a Calendar object that represents that calendar.

The following example uses the date and time elements from the ThaiBuddhistCalendar calendar. DateTime constructors that do not include a calendar parameter assume that the date and time elements are expressed as units in the Gregorian calendar. All other DateTime properties and methods use the Gregorian calendar. For example, the DateTime. Year property returns the year in the Gregorian calendar, and the DateTime. IsLeapYear Int32 method assumes that the year parameter is a year in the Gregorian calendar.

Each DateTime member that uses the Gregorian calendar has a corresponding member of the Calendar class that uses a specific calendar. For example, the Calendar. GetYear method returns the year in a specific calendar, and the Calendar.

IsLeapYear method interprets the year parameter as a year number in a specific calendar. The following example uses both the DateTime and the corresponding members of the ThaiBuddhistCalendar class. It does not include a member that allows you to retrieve the week number of the year. To retrieve the week of the year, call the individual calendar's Calendar.



0コメント

  • 1000 / 1000