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

Kinyanjui Kamau
  • 1,890
  • 10
  • 55
  • 95
  • 2
    Can you show the value of `dateString` please? – Jason Evans Oct 26 '11 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. – Damien_The_Unbeliever Oct 26 '11 at 12:00
  • http://stackoverflow.com/questions/7580809/parse-c-sharp-string-to-datetime – Davide Piras Oct 26 '11 at 12:01
  • dateString "26/10/2011 15:02:01" – Kinyanjui Kamau Oct 26 '11 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. – Jon Skeet Oct 26 '11 at 12:06
  • To paraphrase what @JonSkeet said, your date string doesn't match the format string you specified. If dateString="20111026", then it should parse, otherwise parseExact is doing what is expected. – Suncat2000 Oct 26 '11 at 12:47

6 Answers6

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);
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
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");
Hardik
  • 3,815
  • 3
  • 35
  • 45
Kinjal Patel
  • 420
  • 1
  • 12
  • 20
  • 1
    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... – Jon Skeet Jul 02 '14 at 16:06
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
xanatos
  • 109,618
  • 12
  • 197
  • 280
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.

PVitt
  • 11,500
  • 5
  • 51
  • 85
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.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
0

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

s_nair
  • 812
  • 4
  • 12