Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dates and Times. Slide 2 Introduction to Dates and Times (1) The DateTime data type is used to store dates and times There is a date part There is a time.

Similar presentations


Presentation on theme: "Dates and Times. Slide 2 Introduction to Dates and Times (1) The DateTime data type is used to store dates and times There is a date part There is a time."— Presentation transcript:

1 Dates and Times

2 Slide 2 Introduction to Dates and Times (1) The DateTime data type is used to store dates and times There is a date part There is a time part It’s granular to 100 ns DateTimeOffset works like DateTime but stores a UTC offset The TimeSpan data type is used to store a time interval like a stopwatch You can perform arithmetic on dates and times

3 Slide 3 Introduction to Dates and Times (2) Use System.Convert.ToDateTime to convert a string to a date Use String.Format to format a date as a string ToString() works too

4 Slide 4 The DateTime Data Type (1) There constructors to create A date (year, month, day) A date and time (year, month, day, hours, minutes, seconds) A flag indicating the time zone ( DateTimeKind ) Unspecified UTC Local

5 Slide 5 The DateTime Data Type (2) The value is stored as a 62-bit number Number of elapsed ticks since 1/1/0001 2-bit enum containing the DateTimeKind Local, UTC, or neither When compared, ticks are compared. The DateTimeKind is ignored

6 Slide 6 The DateTime Data Type (Properties) Today gets the current date without the time part Now gets the current date and time (local) UtcNow gets the current date and time in UTC)

7 Slide 7 The DateTime Data Type (Properties) Year, Month, Day, Hour, Minute, Second give us the date and time parts DayofWeek returns an enum DayOfYear returns the value of the same name

8 Slide 8 The DateTimeOffset Data Type Similar to the DateTime data type The constructor takes a TimeSpan argument containing the UTC offset Properties Now gives us the current local time and UTC offset UtcNow adds the UTC offset

9 Slide 9 Time Zones (1) Your computer knows which time zone it’s in

10 Slide 10 Time Zones (2) The System.TimeZone class represents a time zone CurrentTimeZone get the time zone of the local computer StandardName returns the name of the time zone DaylightName returns the name of the daylight time zone

11 Slide 11 Time Zones (3)

12 Slide 12 Date Arithmetic Call AddYears, AddMonths, AddDays, AddHours, AddMinutes, AddSeconds on a date to add the interval units Negative values subtract the incremental unit Example: currentDate = currentDate.AddHours(hours); currentDate = currentDate.AddMinutes(minutes); currentDate = currentDate.AddSeconds(seconds);

13 Slide 13 The TimeSpan Data Type The TimeSpan data type represents a time interval Elapsed hours, minutes, seconds… It’s much like a stopwatch You can Subtract two dates giving a TimeSpan Add a timespan to a date giving a timespan Add and subtract two timespans

14 Slide 14 TimeSpan Constructors The following constructors create a TimeSpan :

15 Slide 15 TimeSpan Properties Days, Hours, Minutes, Seconds, Milliseconds, Ticks get the number of whole intervals in the time period TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds, TotalTicks get the number of whole and fractional intervals in the time period 1:30 is expressed as 1.5 hours

16 Slide 16 Date Arithmetic using TimeSpans Add a TimeSpan to a date: int hours, minutes, seconds; TimeSpan span = new TimeSpan(hours, minutes, seconds); currentDate = currentDate.Add(span); Subtract a TimeSpan from a date currentDate = currentDate.Subtract(span);


Download ppt "Dates and Times. Slide 2 Introduction to Dates and Times (1) The DateTime data type is used to store dates and times There is a date part There is a time."

Similar presentations


Ads by Google