9

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.

7
  • What date should the text "0000-00-00" parse as? Commented Jun 1, 2017 at 5:43
  • You can not change the logic of DateTimeOffset.Parse method. You can write a helper method which will check for the value "0000-00-00" and return current DateTimeOffset else try to parse the input value to DateTimeOffset and return respective value. Commented Jun 1, 2017 at 5:43
  • @ChetanRanpariya So you are saying to use DateTimeOffset.TryParse? Commented Jun 1, 2017 at 5:44
  • @Enigmativity it should parse to the current datetimeoffset Commented Jun 1, 2017 at 5:45
  • @SrimanSaswatSuvankar - Ah, sorry I missed that. Commented Jun 1, 2017 at 5:47

3 Answers 3

7

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;
Sign up to request clarification or add additional context in comments.

8 Comments

0000-00-00 is just a sipmle case.If I give some random string also then it should convert it to DateTimeOffset.now.
@SrimanSaswatSuvankar - So any invalid input should be DateTimeOffset.Now? That is starting to sound weird. How do you control the input?
This is the data I am getting from an external source via API.So no way to control the data.
You need to use DateTimeOffset.TryParse for trying out any random string. Follow the answer below.
@Enigmativity : Yes but chances of bad data is rare and If I turn into a default value then it does not hamper us in any way and it perfectly fits according to the requirements and this bad data was by chance coming from some other system which is not ours.So there is no control what can come in as input.
|
5

You can not change the logic of DateTimeOffset.Parse method. You can write a helper method which will check for the value "0000-00-00" and return current DateTimeOffset else try to parse the input value to DateTimeOffset and return respective value

public class DateTimeOffsetHelper
{
    public static DateTimeOffset FromString(string offsetString)
    {

        DateTimeOffset offset;
        if (!DateTimeOffset.TryParse(offsetString, out offset))
        {
            offset = DateTimeOffset.Now;
        }

        return offset;
    }
}

And you can use it as following.

var offsetString = "2017-05-30";
var offset = DateTimeOffsetHelper.FromString(offsetString);

This should help you resolve you issue.

3 Comments

Slight correction.It should be !DateTimeOffset.TryParse....Because if its not able to parse then its DateTimeOffset.Now
How does TryParse know "05" is month or day? What if "2017-05-04" was passed in?
Tryparse expects "05" to be month only. If that value is more than 12 the TryParse will return false. The output for "2017-05-04" will be "5/4/2017" which is "04-May-2017"
1

Here's a fairly simple one-liner.

Func<string, DateTimeOffset> parseDateTimeOffset = input =>
    input == "0000-00-00" || DateTimeOffset.TryParse(input, out DateTimeOffset output)
        ? DateTimeOffset.Now
        : output;

It is C#7 though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.