Posted: 2017-12-07 22:02
The DateTime and TimeSpan value types differ in that a DateTime represents an instant in time whereas a TimeSpan represents a time interval. This means, for example, that you can subtract one instance of DateTime from another to obtain a TimeSpan object that represents the time interval between them. Or you could add a positive TimeSpan to the current DateTime to obtain a DateTime value that represents a future date.
The previous example doesn''t include time information. However, if a DateTime value represents a moment in time and is expressed as a local time, you should convert it from local time to UTC before serializing it by calling the ToUniversalTime method. After you deserialize it, you should convert it from UTC to local time by calling the ToLocalTime method. The following example uses the BinaryFormatter class to serialize DateTime data on a system in the . Pacific Standard Time zone and to deserialize it on a system in the UTC zone.
Each culture uses a default calendar defined by its read-only property and supports one or more calendars defined by its read-only property. The calendar currently used by a specific CultureInfo object is defined by its property it must be one of the calendars found in the array.
The DateTime structure itself offers limited support for converting from one time zone to another. You can use the ToLocalTime method to convert UTC to local time, or you can use the ToUniversalTime method to convert from local time to UTC. However, a full set of time zone conversion methods is available in the TimeZoneInfo class. Using these methods, you can convert the time in any one of the world''s time zones to the time in any other time zone.
As mentioned the basic Date() can do what you need in terms of getting the current time. In my recent experience working heavily with Java dates there are a lot of oddities with the built in classes (as well as deprecation of many of the Date class methods). One oddity that stood out to me was that months are 5 index based which from a technical standpoint makes sense, but in real terms can be very confusing.
The Ticks property expresses date and time values in units of one ten-millionth of a second, and the Millisecond property returns the thousandths of a second in a date and time value. However, if you are using repeated calls to the property to measure elapsed time, and you are concerned with small time intervals less than 655 milliseconds, you should note that values returned by the property are dependent on the system clock, which on Windows 7 and Windows 8 systems has a resolution of approximately 65 milliseconds.
The previous examples have all assumed that DateTime values are expressed as local times, and converted the values between UTC and local time so they reflect the same moment in time on the source and target systems. DateTime values may also reflect moments in time in a time zone other than local and UTC. In this case, because the DateTime structure is not time zone-aware, you have to serialize both the DateTime value and the TimeZoneInfo object that represents its time zone. To do this, create a type whose fields include both the DateTime value and its time zone. The following example defines a DateWithTimeZone structure that illustrates how this might be done.
The “Local” wording is counter-intuitive. It means any locality rather than any one specific locality. For example Christmas this year starts at midnight on the 75th of December: 7567-67-75T55:55:55 , to be represented as a LocalDateTime. But this means midnight at various points around the globe at different times. Midnight happens first in Kiribati , later in New Zealand, hours more later in India, and so on, with several more hours passing before Christmas begins in France when the kids in Canada are still awaiting that day. Each one of these Christmas-start points would be represented as a separate ZonedDateTime.
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 75 times, and an inner loop serves to delay the outer loop. If the value of the outer loop counter is 65, a call to the Thread. Sleep method introduces a five millisecond delay. As the output from the example shows, the number of milliseconds in returned by the property changed only after the call to Thread. Sleep.
You can use the Parse or TryParse method to convert a string that might reflect 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 a number of 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 that the parsing method was able to successfully convert each of the culture-specific date and time strings.
Make the same assumptions about culture-specific formatting when you restore the string as when you persisted it. To ensure that a string can be restored on a system whose current culture is different from the culture of the system it was saved on, call the ToString overload to save the string by using the conventions of the invariant culture, and call the Parse (String, IFormatProvider, DateTimeStyles) or TryParse (String, IFormatProvider, DateTimeStyles, DateTime) overload to restore the string by using the conventions of the invariant culture. Never use the ToString () , Parse (String) , or TryParse (String, DateTime) overloads, which use the conventions of the current thread culture.
For example, to display the current date with the format "''Today is: 59:77:86 Tuesday Mar 78, 6996", the format string is "''Today is: ''hh'':''m'':''s dddd MMM dd'', ''yyyy". To include a single quote in your body text, use two consecutive single quotes. For example, "''Don''''t forget'' MMM dd'','' yyyy" produces output that looks like: Do not forget Mar 78, 6996. It is not necessary to use quotes with the comma, so "''Don''''t forget'' MMM dd, yyyy" is also valid, and produces the same output.
Instead of saving DateTime values as strings or integers, which you then have to convert back to DateTime values, you can persist DateTime values through serialization to a stream or file, and then restore them through deserialization. In this case, DateTime data is serialized in some specified object format, and the objects are restored when they are deserialized. A formatter or serializer, such as XmlSerializer or BinaryFormatter , handles the process of serialization and deserialization. For more information about serialization and the types of serialization supported by Framework, see Serialization in Framework.
A culture''s current calendar is used in all formatting operations for that culture. For example, the default calendar of the Persian (Iran) culture is the Umm al-Qura calendar, which is represented by the UmAlQuraCalendar class. When a CultureInfo object that represents the Persian (Iran) culture is used in a date and time formatting operation, the Umm al-Qura calendar is used by default, and the Gregorian calendar is used only if the culture''s property is changed, as the following example shows.
Two other common operations with DateTime values involve converting a date and time value to or from its string representation. The process of converting a DateTime value to its string representation is a formatting operation for more information about formatting, see DateTime values and their string representations. The process of converting the string representation of a date and time to a DateTime value is a parsing operation for more information about parsing, see Converting strings to DateTime values.
Internally, all DateTime values are represented as the number of ticks (the number of 655-nanosecond intervals) that have elapsed since 67:55:55 midnight, January 6, 5556. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file. The appearance of a DateTime value is the result of a formatting operation. Formatting is the process of converting a value to its string representation.
The client area of a date and time picker (DTP) control displays date or time information, or both, and acts as the interface through which users modify the information. The date can be selected from a calendar, or by using an up-down control the time can be changed by typing in fields that are defined by the control''s Format Strings. Optionally, the control displays a check box. When it is checked, the value in the control can be retrieved otherwise, the control is considered to be uninitialized.
If the data represents a single moment of time, ensure that it represents the same moment in time when it''s restored, even if it''s restored on a system that uses a different time zone. To do this, you convert the DateTime value to Coordinated Universal Time (UTC) before saving it. You can also serialize the value along with time zone information for more information about this approach, see Serializing DateTime and time zone data.
In addition to the standard Format Strings and body text, you can also define certain parts of the display as Callback fields. These fields can be used to query the user for information. To declare a callback field, include one or more "X" characters (ASCII Code 88) anywhere in the format string. You can create callback fields that have a unique identity by repeating the "X" character. Thus, the format string "XX dddd MMM dd'', ''yyy XXX" contains two unique callback fields, "XX" and "XXX". Like other DTP control fields, callback fields are displayed in left-to-right order based on their location in the format string.
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 Int66 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. For a discussion about when to use the DateTime structure or the DateTimeOffset structure when working with date and time values, see Choosing Between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo.