I am trying to convert string to DateTimeOffset.I am using DatetimeOffset.Parse(string).Parse obviously throws an exception when string is not in correct format.It is not able to parse 0000-00-00.
I want a single line of code saying me the best possible way to tackle this situation.If input is 0000-00-00 then it should be converted to current DateTimeOffset.
Any other string also apart from 0000-00-00 that cant be parsed should be changed to DateTimeOffset.Now.
Parse(String, IFormatProvider, DateTimeStyles) Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information and formatting style.
DateTime values lack any knowledge of time zone, or lack thereof. If you need to know when things actually occurred, with more precision than just the approximate date, and you can't be 100% sure that your dates are ALWAYS stored in UTC, then you should consider using DateTimeOffset to represent your datetime values.
With its Kind property, DateTime is able to reflect only Coordinated Universal Time (UTC) and the system's local time zone. DateTimeOffset reflects a time's offset from UTC, but it does not reflect the actual time zone to which that offset belongs.
For UTC and local DateTime values, the Offset property of the resulting DateTimeOffset value accurately reflects the UTC or local time zone offset.
When you convert a datetimeoffset value to datetime, the date and time values are copied, and the time zone offset is truncated. When the fractional precision of the datetimeoffset value is greater than three digits, the value is truncated.
The method's single parameter is the DateTime value that represents the date and time to be converted. If the time zone supports daylight saving time, this parameter allows the method to determine the appropriate offset for that particular date and time. The DateTime property is most commonly used to perform DateTimeOffset to DateTime conversion.
String To DateTime Conversion In C#. 1 C# DateTime Conversion. C# DateTime is a struct type, which is mostly used in applications to manage date, date-time, time data types. Most of time, ... 2 Convert.ToDateTime () 3 DateTime.Parse () 4 DateTime.ParseExact () 5 DateTime.TryParse () More items
Converts the DateTimeOffset value to UTC and outputs it using the format yyyy-MM-dd HH:mm:ssZ. The remaining standard date and time format specifiers behave the same with the ToString (String) method as they do with the ToString method.
If I understand your question correctly, you are looking for
DateTimeOffset dto = (input == "0000-00-00" ? DateTimeOffset.Now : DateTimeOffset.Parse(input));
EDIT Based on your clarification that all invalid dates should default to the current time, the following will do that:
DateTimeOffset dto;
if(!DateTimeOffset.TryParse(input, out dto))
dto = DateTimeOffset.Now;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With