Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert string to DateTimeOffset?

Tags:

c#

c#-4.0

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.

like image 670
Sriman Saswat Suvankar Avatar asked Jun 01 '17 05:06

Sriman Saswat Suvankar


People also ask

How to convert string to DateTimeOffset?

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.

Why use DateTimeOffset instead of DateTime?

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.

What is difference between DateTime and DateTimeOffset?

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.

Is DateTimeOffset UTC?

For UTC and local DateTime values, the Offset property of the resulting DateTimeOffset value accurately reflects the UTC or local time zone offset.

What happens when you convert a datetime offset to datetime?

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.

What is datetimeoffset in datetime method?

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.

How to convert string to datetime in C?

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

How do I convert datetimeoffset to UTC in Java?

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.


1 Answers

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;
like image 117
Dan Def Avatar answered Oct 21 '22 21:10

Dan Def