aAbEACBBtBAsXY-~| 9555V 65FWC 47466V~BC 4746VB~ 47466VCC 47466G76V 78986VY-~~~~~~~ZCBZBWCZY-~~~B~BZ 7F5 9ZZZYZZZZBZZZ-

DateTime.` Each day the sun rises. Bright light marks a new day. A yellow bird searches for food. From Earth's surface we view the sun's path. What begins as motion gains meaning as time.`A .NET developer` uses DateTime—with TimeSpan and Stopwatch we manipulate time. Night comes fast in this world. Time must be handled with care. `TimeSpan `timespan`Stopwatch `stopwatch`Constructor, new.` We call the instance DateTime constructor. The arguments must match a real date that occurred. This is a validating constructor. `Example: `This example writes a DateTime to the console. It also compares a DateTime against the Today value.`Un-representable error.` The DateTime constructor validates possible arguments. If we try to create a DateTime that cannot exist, we get an argument exception. `Yesterday.` Here we subtract one day from the current day. We do this by adding -1 to the current day. This is necessary because no "Subtract Days" method is provided. `The example was run a few years ago. The output will naturally vary depending on the day you run it.`DateTime.Today is always set to the machine's local time, which depends on the current system.`Tomorrow.` To figure out tomorrow, we add one using the Add() method. This is useful with date queries in databases. We use the AddDays method. `Static: `GetTomorrow is a static method—it does not require state to be saved. DateTime.Today is also static.`Static `static`DateTime.Add uses offsets. It accepts both negative and positive numbers. Here we go back in time.`First day.` We use a helper method to find the first day in a year. We use an overloaded method. With overloading, we often can use methods in an easier, clearer way. `Overload `overload`Current: `If we want the current year, we can call FirstDayOfYear with no parameter. The year from Today will be used.`Last day.` Here we find the last day in any year. Leap years make this more complicated, as February may have 28 or 29 days. We must programmatically find the year's length. `This method is ideal for when you want to count days, as for a database range query for a certain year.`Tip 2: `It is better to use the DateTime constructor, rather than DateTime.Parse. This is faster and has clearer syntax.`DaysInMonth.` Many static methods are also available on the DateTime class. With DaysInMonth we look up the number of days in a month based on the year. `Elapsed.` Next, we find the "age" of a certain date, and how long ago it was in time. We can do this with DateTime.Subtract, which will return a TimeSpan. `DateTime, Elapsed `datetime-elapsed`DateTime Subtract `datetime-subtract`Methods.` Many DateTime methods receive double type arguments. A double is a numeric type used like an int. Doubles can store decimal places. `Add: `The Add method (and Subtract) requires a TimeSpan argument. We must first use the TimeSpan constructor.`AddDays: `Receives a double integer, which adds or subtracts days. We can use AddHours, AddMinutes, AddSeconds and more.`AddTicks: `One tick is considered one millisecond. This method might be useful when used with Environment.AddTicks.`FromBinary, ToBinary: `Parses or creates a binary date. You may have a binary date if you have serialized a date to a file.`GetDaylightSavingTime: `Daylight saving time changes the daylight hours depending on the season.`IsLeapYear: `Leap years have 29 days in February. This must be accounted for to have correct programs.`ToLocalTime: `Normally your dates will be in the local time. You can convert an external DateTime to the local time zone with this.`FromOADate.` Dates are stored in many formats. MS Excel stores dates in a special numeric format. We use FromOADate to read in these dates. And we can output this format with ToOADate. `FromOADate `fromoadate`FromOADate and ToOADate are useful for converting Excel dates to C# dates. May also be useful for Visual FoxPro or Microsoft Access.`Excel `excel`Parse.` It is possible to parse DateTime instances. This converts a string value into a DateTime. The "Try" methods avoid expensive exceptions on invalid strings. `DateTime.Parse `datetime-parse`DateTime.TryParse `datetime-tryparse`ToString.` A DateTime can be converted into a string. This helps when displaying, writing or post-processing. With ToString we easily format a DateTime struct as text. `These methods include ToString, ToLongDateString, ToLongTimeString, ToShortDateString and ToShortTimeString.`Note 2: `We must experiment to find the most appropriate method. This is usually the most compatible one.`Months, days.` These examples deal with using months and days. We access the Month property. We get all of the days of the week. And we store arrays of all months and days. `Month `datetime-month`DayOfWeek `dayofweek`Month, Day Arrays `month-day-arrays`Properties.` Let us look at properties on the DateTime type. These properties, also listed at MSDN, are useful abstractions. They return specific aspects of your DateTime. `Date: `This returns only the date component of the DateTime. It has the "time value set to 12 midnight (00:00:00)."`Day, Month, Year: `These return a component of the time. Note that this is not the interval since any other date.`Now, UtcNow: `These return the current DateTime, with all of the fields correctly filled. DateTime.Now is a useful property.`DateTime.Now `datetime-now`Today: `Gets the current date in the form of a DateTime instance. The Today property contains no time information.`DateTime.Today `datetime-today`MinValue, null.` A DateTime cannot be assigned to null. It is a struct, and like an int cannot be null. We can use a special value like DateTime.MinValue to initialize an empty DateTime. `MinValue `datetime-minvalue`Nullable.` We can never have a null DateTime instance. DateTime is a value type. But we can use a nullable DateTime. We use a question mark "DateTime?" for this type. `Nullable DateTime: DateTime? `nullable-datetime`TimeZone.` We all live in different locations. With TimeZone we can easily access information about time zones. Sadly TimeZone is not used in most programs. `TimeZone `timezone`Format.` We format DateTime instances when we want to store the DateTime in a text representation. Humans read text, not bytes in structs. `DateTime: Formats `datetime-format`DateTime: File Names `filename-datetime`DateTime: SQL `datetime-sql`Pretty printing.` We show how to format DateTime values in more complex ways. We format DateTimes in a "pretty" way—for example, like "90 seconds ago." `Pretty Date `pretty-date`24 hours clock.` It is possible to format times on a 24-hour clock format. This is sometimes called military time. Some format codes can be used for this. `24-Hour Time Formats `24-hour-time`Timer.` With this class, we create a recurrent event, based on an interval. Timer helps monitor long-running processes such as websites. `Timer `timer`Stopwatch.` If we need to benchmark a program, we use the Stopwatch. For some types of programs, we can use Stopwatch to measure startup time. `Stopwatch `stopwatch`Stopwatch: Benchmarking `benchmark`Closest date.` How can we compute the distance of DateTimes? With some logic we can find the closest date to any specific date. This could be useful someday. `Closest Date `closest-date`Sort.` An array or List of DateTime structs can be sorted. This will go from low to high (early to later) in an ascending sort by default. `Sort DateTimes `sort-datetime`Performance.` Here is a way to optimize DateTime. When a method calls DateTime.Now, we can sometimes cache this value. This prevents excessive time queries. `DateTime Performance `datetime-performance`A summary.` Time is a complex subject. But with DateTime and TimeSpan we represent it with relative ease in our programs. These types are value types (like ints) not reference types.

NHBXkBBHDHQHbHJHAB{BNH%N{kBNNH{This H. is constructed with an instance constructor.BNNH9We write itHjthe console.BNNH9If this is today, the second line will be True.BNNkH. HhHyHqkH.k(k2017k, k1k, k18k);BNNH'Hh);BNNH'HhHxH..Today);BN}B}BBkBB1/18/2017 12:00:00 AMBFalsekBBHDHQHbHJHAB{BNH%N{kBNNH{This will cause an error.BNNkH. xHyHqH.(k-1k, 1, 1);BN}B}BBkResultskBBAn unhandled exception of type 'HQ.ArgumentOutOfRangeH+'BoccurredHpmscorlib.dllBBAdditional inH|mation: Year, Month,HVDay parameters describe anBun-representable H..kBBHDHQHbHJHAB{BNH%N{BNNH'kXToday: {0}Xk, H..Today)HbNNkH.k yHyGetYesterday();BNNH'kXYesterday: {0}Xk, y);BN}kBBN/H{<H7>BN/H{Gets the previous dayHjthe current day.BN/H{</H7>BNkH?H. GetYesterday()BN{kBNNH{Add -1Hjnow.BNNkHK H..Today.AddDays(-1);BN}B}BBkBBToday: 11/30/2008 12:00:00 AMBYesterday: 11/29/2008 12:00:00 AMkBBHDHQHbHJHAB{BNH%N{BNNH'kXToday: {0}Xk, H..Today)HbNNkH.k dHyGetTomorrow();BNNH'kXTomorrow: {0}Xk, d);BN}kBBN/H{<H7>BN/H{Gets the next day, tomorrow.BN/H{</H7>BNkH?H. GetTomorrow()BN{BNNHK H..Today.AddDays(1);BN}B}BBkBBToday: 11/30/2008 12:00:00 AMBTomorrow: 12/1/2008 12:00:00 AMkBBHDHQHbHJHAB{BNH%N{BNNH'kXFirst day: {0}Xk, FirstDayOfYear())HbNNkH.k dHyHqH.(1999, 6, 1);BNNH'kXFirst day of 1999: {0}Xk, FirstDayOfYear(d));BN}kBBN/H{<H7>BN/H{Gets the first day of the current year.BN/H{</H7>BNkH?H. FirstDayOfYear()BN{BNNHK FirstDayOfYear(H..Today);BN}kBBN/H{<H7>BN/H{Finds the first day of year of the specified day.BN/H{</H7>BNkH?H. FirstDayOfYear(H. y)BN{BNNHK HqH.(y.Year, 1, 1);BN}B}BBkBBFirst day: 1/1/2008 12:00:00 AMBFirst day of 1999: 1/1/1999 12:00:00 AMkBBHDHQHbHJHAB{BNH%N{BNNH'kXLast day: {0}Xk, LastDayOfYear())HbNNkH.k dHyHqH.(1999, 6, 1);BNNH'kXLast day of 1999: {0}Xk, LastDayOfYear(d));BN}kBBN/H{<H7>BN/H{Finds the last day of the year Hwtoday.BN/H{</H7>BNkH?H. LastDayOfYear()BN{BNNHK LastDayOfYear(H..Today);BN}kBBN/H{<H7>BN/H{Finds the last day of the year Hwthe selected day's year.BN/H{</H7>BNkH?H. LastDayOfYear(H. d)BN{kBNNH{Hkfirst of next year.BNNkH.k nHyHqH.(d.YearH}1, 1, 1);kBBNNH{Subtract one from it.BNNkHK n.AddDays(-1);BN}B}BBkBBLast day: 12/31/2008 12:00:00 AMBLast day of 1999: 12/31/1999 12:00:00 AMkBBHDHQHbHJHAB{BNH%N{BNNHidaysHyH..kDaysInMonthk(k2014k, k9k);k H{September.BNNkH'days)HbNNdaysHyH..kDaysInMonthk(k2014k, k2k);k H{February.BNNkH'days);BN}B}BBkBB30B28kBBHDHQHbHJHAB{BNH%N{BNNH. currentHyknullk;BN}B}BBkResultskBBkerror CS0037k: Cannot cHN nullHj'H.'Bbecause it is a non-nullable Hh typeBBkBBHJHAB{BNH%N{kBNNH{This program can be compiled.BNNH9Use MinHX instead of null.BNNkH. currentHykH..MinHXk;BN}B}k

)d?2W..[:aDateTime constructorshows un-representable DateTimecomputes yesterdayAddDaysgets first dayfinds last dayDaysInMonthcauses null DateTime errorDateTime.MinValue