How to pass DateTime C#

Posted on 2009-05-20
Last Modified: 2012-05-07
I have the following date 2009-05-19T00:00:00 i wish to pass to a date. But i'm getting

String was not recognized as a valid DateTime

string myDateString = "2009-05-19T00:00:00";

Date date = DateTime.Parse(myDateString);

Question by:frosty1
  • 3
  • 2
LVL 22

Expert Comment

ID: 24431666
the T is probably throwing it of
LVL 39

Accepted Solution

abel earned 500 total points
ID: 24431723
The T is good there, it is the UTC format of a date, but it is missing the +HHMM for the offset to GMT.

Hmm, just tried, both the following lines works correctly for me:

// correct parsing of date strigns
DateTime dt1 = DateTime.Parse("2009-05-19T00:00:00+0100");
DateTime dt2 = DateTime.Parse("2009-05-19T00:00:00");

Open in new window

LVL 39

Expert Comment

ID: 24431732
PS: what type of object is your "Date" btw. In VB is exists, standard, but in C# it does not...
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Author Comment

ID: 24431851
thanks abel,

Sorry i meant DateTime.

Turns out that i occasionally string myDateString was empty. Your clarification help me find this. Very helpful.
LVL 39

Expert Comment

ID: 24431968
glad you found it. You can use TryParse if the string can be malformed. The return value is a boolean which you can use to report the error, instead of having a raised exception.

Author Comment

ID: 24432155
hmm yeah, that would be alot neater. thanks again.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question