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...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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