| Start |
|---|
| DateTime Formating Standard DateTime Formatting DateTimeFormatInfo specifies a set of specifiers for simple date and time formating. Every specifier correspond to a particular DateTimeFormatInfo format pattern. Custom DateTime Formatting There are following custom format specifiers:
You can use also date separator For code example (opens new window) For more information MSDN (opens new window). |
| DateTime.AddDays(Double) Add days into a dateTime object. You also can subtract days passing a negative value: |
DateTime.AddHours(Double) |
DateTime.Parse(String) |
DateTime.TryParse(String, DateTime) |
DateTime.AddMilliseconds(Double) |
DateTime.Compare(DateTime t1, DateTime t2 ) |
DateTime.DaysInMonth(Int32, Int32) |
| DateTime.AddYears(Int32) Add years on the dateTime object: |
| Pure functions warning when dealing with DateTime Wikipedia currently defines a pure function as follows:
As a developer you need to be aware of pure methods and you will stumble upon these a lot in many areas. One I have seen that bites many junior developers is working with DateTime class methods. A lot of these are pure and if you are unaware of these you can be in for a suprise. An example: Given the example above one may expect the result printed to console to be '26/12/2016' but in reality you end up with the same date. This is because AddDays is a pure method and does not affect the original date. To get the expected output you would have to modify the AddDays call to the following: |
| DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded. For Example Parse date with no style flags. Use custom formats with M and MM. Parse a string with time zone information. Parse a string represengting UTC. Outputs |
DateTime.Add(TimeSpan) |
| Parse and TryParse with culture info You might want to use it when parsing DateTimes from different cultures (languages) (opens new window), following example parses Dutch date. Example of Parse: |
DateTime as initializer in for-loop Iterating on a |
DateTime ToString, ToShortDateString, ToLongDateString and ToString formatted |
| Current Date To get the current date you use the For example: Writes today's date, in your local format to the console. |
| DateTime.ParseExact(String, String, IFormatProvider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. Convert a specific format string to equivalent DateTime Let's say we have a culture-specific DateTime string Convert a date time string to equivalent Let's say we have a DateTime string in Convert a date time string to equivalent DateTime object without any specific culture format with different format Let's say we have a Date string , example like '23-12-2016' or '12/23/2016' and we want it to convert to equivalent NOTE : |
Read Article
https://develop.horje.com/learn/1434/reference