How to pass DateTime C#

Posted on 2009-05-20
Medium Priority
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 2000 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...
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.


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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

600 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