WhoAmI
WhoAmI

Reputation: 1236

Error converting DateTime to string format yyyy-MM-dd

I'm trying to convert some DateTime values to string format yyyy-MM-dd. The problem is i only need the date but in my case model.StartDate contains both date and time. When declaring model.StartDate as string "start" looks like this: 4/1/2014 12:00:00 AM. I get this error when trying to parse:

System.FormatException was unhandled by user code Message=String was not recognized as a valid DateTime.

My best guess is that the error occurs because string contains both Date and Time but i could be wrong. If i explore model.StartDate further i can also find Day, DayOfTheWeek etc. Is this the right approach? I just want to convert model.StartDate to string "start" with format yyyy-MM-dd.

Heres my code:

string start = model.StartDate.ToString();
model.StartDate = DateTime.ParseExact(start, "yyyy-MM-dd", CultureInfo.InvariantCulture);

string end = model.EndDate.ToString();
model.EndDate = DateTime.ParseExact(end, "yyyy-MM-dd", CultureInfo.InvariantCulture);

Dunno what the problem is, might be that start contains time? I have no idea.

The model.StartDate and model.EndDate are DateTime properties from the view model:

        [NopResourceDisplayName("Admin.GAStatistics.GAStatistics.StartDate")]
        [UIHint("DateNullable")]
        public DateTime? StartDate { get; set; }

        [NopResourceDisplayName("Admin.GAStatistics.GAStatistics.EndDate")]
        [UIHint("DateNullable")]
        public DateTime? EndDate { get; set; }

EDIT: Iv'e uploaded a image here showing the actual output i'm getting in the debugger: https://imageshack.com/i/1n51u2p

Thank you

Upvotes: 1

Views: 5605

Answers (3)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131601

You are converting the dates to string but you don't specify the format. Try

string start = model.StartDate.ToString("yyyy-MM-dd);

ToString() uses the current thread's Culture format to convert the date to a string, including the time. The format used is G, the general date and time format.

Just for this format, you don't need to specify CultureInfo.InvariantCulture because there isn't anything culture specific. A common gotcha with the yyyy/MM/dd format though is that some cultures use - as the date specifier, and / is the date placeholder. In such a case you would have to use:

string start = model.StartDate.ToString("yyyy-MM-dd,CultureInfo.InvariantCulture);

UPDATE

From the comments, it seems that model.StartDate and model.EndDate are not DateTime objects but strings with a specific format that include a time element.

What you are actually trying to do is parse the original string to a DateTime object, then format this object to the new format string:

var date=DateTime.ParseExact(model.StartDate,"M/d/YYYY HH:mm:ss tt",
         CultureInfo.InvariantCulture);
model.StartDate=date.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture);

assuming the string the value "4/1/2014 12:00:00 AM" for April 1, 2014

Upvotes: 3

James
James

Reputation: 82136

You appear to be misunderstanding how ParseExact works (or actually what it does). Parsing, in general, is the process of taking data of type X and converting it to type Y - in the context of DateTime this means converting a date string to a DateTime instance. This is completely different to what you are trying to do which is formatting a DateTime instance.

Given you already have the date you don't need to parse anything, all you need to do is format the date

model.StartDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

CultureInfo.InvariantCulture is important when working with fixed formats because you want to make sure you aren't culture aware i.e. the format you specify is exactly how you want it to display in all cultures.

Upvotes: 1

Laoujin
Laoujin

Reputation: 10239

Use the .Date property of a DateTime to get only the Date part. Your ToString() will also yield different results based on the current culture meaning that while your ToString() and then TryParse might work for you right now, it will break in other countries.

You can use ToString() overload to specify a specific format. Different formats can be found here

Upvotes: -1

Related Questions