-1

I have a Nullable DateTime, and I got an error :

Additional information: String was not recognized as a valid DateTime.

I looked at here, here,here and also here . I also tried String.Format("{0:s}", dateTime), but it does not change my DateTime format.My code is like below,

if (person.JsonData.PasswordChangeRequestTime != null)
{
     DateTime data;  
     data = DateTime.ParseExact(((DateTime)person.JsonData.PasswordChangeRequestTime).Date.ToStringDateTime(), "dd'-'MM'-'yyyy HH':'mm':'ss", CultureInfo.InvariantCulture);   
     person.setColumnValue("passwordchangerequesttime", data);
}

One of my DateTime is like this: 1/1/2015 2:00:00 PM I want them in a format of 1-1-2015 14:00:00 what is wrong with my DateTime.ParseExact function?

By the way, I do not want to use subString function!

Community
  • 1
  • 1
Zahra Aminolroaya
  • 520
  • 1
  • 5
  • 28

1 Answers1

2

You don't need to do anything.

Your (DateTime)person.JsonData.PasswordChangeRequestTime already a DateTime, what you see this is probably in a debugger or something.

A DateTime does not have any implicit format. It just have date and time values. Format concept only matter when you get it's textual (string) representation which is usually done with DateTime.ToString() method.

If you wanna get exact string representation of it, you can use ToString method with proper format and culture settings like;

((DateTime)person.JsonData.PasswordChangeRequestTime)
                          .ToString("d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture)

genereates 1/1/2015 2:00:00 PM and

((DateTime)person.JsonData.PasswordChangeRequestTime)
                          .ToString("d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture)

generates 1-1-2015 14:00:00 formatted strings.

If your 1/1/2015 2:00:00 PM is string, not a DateTime, you need to parse it to DateTime with proper format first then generate it's string representation with proper format as well.

string s = "1/1/2015 2:00:00 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    dt.ToString("d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    // Generates 1-1-2015 14:00:00
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • thank @Soner Gönül! One of my DateTime is like this: `1/1/2015 2:00:00 PM` I want them in a format of `1-1-2015 14:00:00`!!! it does not work for `((DateTime)person.JsonData.PasswordChangeRequestTime)`!!! By the way, I do not want to use `subString` function! – Zahra Aminolroaya Nov 02 '15 at 06:28
  • I have also tried `data = ((DateTime)person.JsonData.PasswordChangeRequestTime).ToString("d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture)` but there is an error :`Cannot implicitly convert type 'string' to 'system.DateTime'` – Zahra Aminolroaya Nov 02 '15 at 06:32
  • @DooshizePlusPlus Edited my answer. Take a look. Your code won't compile because `ToString` method returns `string` but you try to assign it to `data` which is `DateTime` and there is no implicit conversation between them. – Soner Gönül Nov 02 '15 at 06:34
  • @SonerGönül I suspect the concept of "DateTime does not include formatting" is way too complex for most people. I'm not sure if you could have made your post better to explain it so :( – Alexei Levenkov Nov 02 '15 at 06:38
  • Thanks! I used `person.setColumnValue("passwordchangerequesttime", ((DateTime)person.JsonData.PasswordChangeRequestTime).ToString("d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture))`! and @SonerGönül the format is exactely `DateTime` not `string`! – Zahra Aminolroaya Nov 02 '15 at 06:44
  • @AlexeiLevenkov Amen to that. No matter how clear you try explain, some people _just_ won't get it. This is the best explanation I can made, so.. :) – Soner Gönül Nov 02 '15 at 06:46