4

I am trying to save the DateTime.Now in the format "yyyyMMdd"

I have this code

string todaysDate = DateTime.Now.ToString();

...

U_Date_of_PD_added = GetDateFromString(todaysDate) 

// U_Date_of_PD_added is a datetime Database field

...

//Method to get date from string
private DateTime GetDateFromString(string dateString)
{
   string format = "yyyyMMdd";
   return DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
}

I keep getting the error "String was not recognized as a valid DateTime." as it tries to parse. What could be wrong?

I do not care if it saves the time, I would prefer 00:00:00.000

6
  • 2
    Can you show the value of dateString please? Commented Oct 26, 2011 at 12:00
  • 4
    Why convert the datetime into a string? Almost every data access technology in existence lets you pass parameters as their correct datatype, and deals with marshalling those values to the database, so that you don't have to fiddle with this kind of thing. Commented Oct 26, 2011 at 12:00
  • stackoverflow.com/questions/7580809/… Commented Oct 26, 2011 at 12:01
  • dateString "26/10/2011 15:02:01" Commented Oct 26, 2011 at 12:04
  • 3
    @KinyanjuiKamau: So this is clearly failing in your very first sentence - you're not managing to save DateTime.Now in the format "yyyyMMdd" and that problem happens before parsing. Commented Oct 26, 2011 at 12:06

6 Answers 6

11

You also need to give the format string when you convert the date to a string:

string todaysDate = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

2
DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

will give you the current date. Note that it's important the CultureInfo.InvariantCulture. If you don't trust it, try:

var r = DateTime.Now.ToString("yyyyMMdd", new CultureInfo("ar-SA")); // 14321128
var r2 = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture); // 20111026

1 Comment

+1: That "ar-SA" culture info is a good test case, and I've updated my answer.
2

First you get today's date and then convert in string as follow and you are done.

DateTime today = DateTime.Today.Date;
string date = today.ToString("yyyy/MM/dd");

1 Comment

That isn't even valid code (you don't have a valid string literal)... and unless you specify the culture, you may well get a result you don't expect. Additionally, please pay attention to formatting when you post. Your code isn't shown as code...
1

DateTime.Now.ToString() results in a format that is region specific, but it mostly contains the time. This format is called "General date/time pattern (long time)."

So trying to parse it with your format string will fail because your format string does not contain information about the time component.

You can use DateTime.TryParse without providing a format string or you have to provide your format string to your call to DateTime.Now.ToString as well.

Comments

1

The value of todaysDate cannot be converted to a DateTime object by using DateTime.Now.ToString() because DateTime.Now.ToString() results in a value that uses the default date format (26-10-2011 14:02:31). Try doing this instead: string todaysDate = DateTime.Now.ToString("yyyyMMdd"); and you should be able to convert it back using your GetDateFromString method.

Comments

0

DateTime.Now.ToString("yyyyMMdd") will return a string of 20110926.

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.