In this post i try to show you how to use the .NET object DateTime: how to get the current date and time.
How you can find your expected output....
Console.WriteLine("Current date and time: {0}", System.DateTime.Now);
Console.WriteLine("Subtracted 15 seconds: {0}", System.DateTime.Now.AddSeconds(-15));
Console.WriteLine("Subtracted 32 minutes: {0}", System.DateTime.Now.AddMinutes(-32));
Console.WriteLine("Subtracted 8 days: {0}", System.DateTime.Now.AddDays(-8));
Console.WriteLine("ADD 5 months: {0}", System.DateTime.Now.AddMonths(5));
Console.WriteLine("Subtracted 5 months: {0}", System.DateTime.Now.AddMonths(5));
Console.ReadLine();
DateTime.Add
This requires a TimeSpan to be received. You will need to use the TimeSpan constructor first.
DateTime.AddDays
Receives a positive or negative double integer, which adds or subtracts days. We see examples in this document.
DateTime.AddHours
DateTime.AddMilliseconds
DateTime.AddMinutes
DateTime.AddMonths
DateTime.AddSeconds
DateTime.AddYears
These are self-explanatory and receive a positive or negative double, for adding or subtracting the specified part of the DateTime.
DateTime.AddTicks
One tick is considered one millisecond. This method might be useful when used with Environment.AddTicks.
DateTime.Compare
DateTime.CompareTo
These tell you whether one date is bigger or smaller than another. Mainly used for sorting. However, you don't need to implement a custom sort for DateTime normally.
DateTime.DaysInMonth
Helper method that will tell you how many days are in a month. I haven't used it but it is useful to know about.
DateTime.Equals
This is the sample as op_Equality, the == operator.
DateTime.FromBinary
DateTime.ToBinary
Parses or creates a binary date. You may have a binary date if you have serialized a date to a file. I haven't used these.
DateTime.FromFileTime
DateTime.FromFileTimeUtc
DateTime.ToFileTime
DateTime.ToFileTimeUtc
Use these for when you have file times you need to convert. I haven't used this.
DateTime.FromOADate
DateTime.ToOADate
Useful for converting Excel dates to C# dates. May also be useful for Visual FoxPro or Microsoft Access. I have used this for Microsoft.Office.Excel.Interop.
DateTime.GetDateTimeFormats
This provides functionality related to formatting. It is overloaded and may be helpful rarely.
DateTime.GetDaylightSavingTime
Daylight saving time is what we get for letting our politicians pretend to be scientists. Not useful in Arizona.
DateTime.IsLeapYear
Leap years have 29 days in February. Leap year is here to make programmers' lives hard and has very little other impact.
DateTime.Subtract
Takes away one DateTime or TimeSpan from the first one. This isn't as useful as the Add methods, as you must provide a more complex parameter.
DateTime.ToLocalTime
Normally your dates will be in the local time, but if you acquire an external DateTime, you can convert it to the local timezone with this.
DateTime.Parse
DateTime.ParseExact
DateTime.TryParse
DateTime.TryParseExact
You will need to consult MSDN for information on exactly what formats can be parsed. If you have lots of invalid data, use TryParse, as it will capture its exceptions and improve performance.
DateTime.ToString
DateTime.ToLongDateString
DateTime.ToLongTimeString
DateTime.ToShortDateString
DateTime.ToLongTimeString
For these ToString methods, it is best to simply experiment to find the one you like best or that is most compatible. I also recommend checking MSDN's articles on DateTime format strings.
DateTime.Date
This returns only the date component of the DateTime. It has the "time value set to 12:00:00 midnight (00:00:00)." From DateTime.Date Property (System) at MSDN.
DateTime.Day
DateTime.DayOfWeek
DateTime.DayOfYear
DateTime.Hour
DateTime.Millisecond
DateTime.Minute
DateTime.Month
DateTime.Second
DateTime.Ticks
DateTime.TimeOfDay
DateTime.Year
These return a component of the time. Note that this is not the interval since any other date. They just return the single part of the date. For example, Day returns "the day component, expressed as a value between 1 and 31." From DateTime.Day Property (System) at MSDN.
DateTime.Now
DateTime.UtcNow
These return the current DateTime, with all of the fields correctly filled in. DateTime.Now is one of the most common properties to use.
DateTime.Kind
Returns a DateTimeKind. I recommend checking MSDN for all the specifics, as they are not useful for me to repeat here.
How you can find your expected output....
Console.WriteLine("Current date and time: {0}", System.DateTime.Now);
Console.WriteLine("Subtracted 15 seconds: {0}", System.DateTime.Now.AddSeconds(-15));
Console.WriteLine("Subtracted 32 minutes: {0}", System.DateTime.Now.AddMinutes(-32));
Console.WriteLine("Subtracted 8 days: {0}", System.DateTime.Now.AddDays(-8));
Console.WriteLine("ADD 5 months: {0}", System.DateTime.Now.AddMonths(5));
Console.WriteLine("Subtracted 5 months: {0}", System.DateTime.Now.AddMonths(5));
Console.ReadLine();
DateTime.Add
This requires a TimeSpan to be received. You will need to use the TimeSpan constructor first.
DateTime.AddDays
Receives a positive or negative double integer, which adds or subtracts days. We see examples in this document.
DateTime.AddHours
DateTime.AddMilliseconds
DateTime.AddMinutes
DateTime.AddMonths
DateTime.AddSeconds
DateTime.AddYears
These are self-explanatory and receive a positive or negative double, for adding or subtracting the specified part of the DateTime.
DateTime.AddTicks
One tick is considered one millisecond. This method might be useful when used with Environment.AddTicks.
DateTime.Compare
DateTime.CompareTo
These tell you whether one date is bigger or smaller than another. Mainly used for sorting. However, you don't need to implement a custom sort for DateTime normally.
DateTime.DaysInMonth
Helper method that will tell you how many days are in a month. I haven't used it but it is useful to know about.
DateTime.Equals
This is the sample as op_Equality, the == operator.
DateTime.FromBinary
DateTime.ToBinary
Parses or creates a binary date. You may have a binary date if you have serialized a date to a file. I haven't used these.
DateTime.FromFileTime
DateTime.FromFileTimeUtc
DateTime.ToFileTime
DateTime.ToFileTimeUtc
Use these for when you have file times you need to convert. I haven't used this.
DateTime.FromOADate
DateTime.ToOADate
Useful for converting Excel dates to C# dates. May also be useful for Visual FoxPro or Microsoft Access. I have used this for Microsoft.Office.Excel.Interop.
DateTime.GetDateTimeFormats
This provides functionality related to formatting. It is overloaded and may be helpful rarely.
DateTime.GetDaylightSavingTime
Daylight saving time is what we get for letting our politicians pretend to be scientists. Not useful in Arizona.
DateTime.IsLeapYear
Leap years have 29 days in February. Leap year is here to make programmers' lives hard and has very little other impact.
DateTime.Subtract
Takes away one DateTime or TimeSpan from the first one. This isn't as useful as the Add methods, as you must provide a more complex parameter.
DateTime.ToLocalTime
Normally your dates will be in the local time, but if you acquire an external DateTime, you can convert it to the local timezone with this.
DateTime.Parse
DateTime.ParseExact
DateTime.TryParse
DateTime.TryParseExact
You will need to consult MSDN for information on exactly what formats can be parsed. If you have lots of invalid data, use TryParse, as it will capture its exceptions and improve performance.
DateTime.ToString
DateTime.ToLongDateString
DateTime.ToLongTimeString
DateTime.ToShortDateString
DateTime.ToLongTimeString
For these ToString methods, it is best to simply experiment to find the one you like best or that is most compatible. I also recommend checking MSDN's articles on DateTime format strings.
More properties
DateTime.Date
This returns only the date component of the DateTime. It has the "time value set to 12:00:00 midnight (00:00:00)." From DateTime.Date Property (System) at MSDN.
DateTime.Day
DateTime.DayOfWeek
DateTime.DayOfYear
DateTime.Hour
DateTime.Millisecond
DateTime.Minute
DateTime.Month
DateTime.Second
DateTime.Ticks
DateTime.TimeOfDay
DateTime.Year
These return a component of the time. Note that this is not the interval since any other date. They just return the single part of the date. For example, Day returns "the day component, expressed as a value between 1 and 31." From DateTime.Day Property (System) at MSDN.
DateTime.Now
DateTime.UtcNow
These return the current DateTime, with all of the fields correctly filled in. DateTime.Now is one of the most common properties to use.
DateTime.Kind
Returns a DateTimeKind. I recommend checking MSDN for all the specifics, as they are not useful for me to repeat here.
No comments:
Post a Comment