Hi I'm converting a string to DateTime
variable but getting exception. Please tell me what is wrong in this method?
string str = "24-04-2014T15:18:18";
DateTime dtStartDateTime = DateTime.ParseExact(stime, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
Exception is:
String was not recognized as a valid DateTime.
Use dd-MM-yyyyTHH:mm:ss
format instead. Your string and format doesn't match exactly.
From DateTime.ParseExact
method
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
string str = "24-04-2014T15:18:18";
DateTime dtStartDateTime = DateTime.ParseExact(str,
"dd-MM-yyyyTHH:mm:ss",
CultureInfo.InvariantCulture);
Here a demonstration
.
By the way, I think your stime
should be str
:)
OK, so if I am understanding the question, I think this is what you need:
string str = "24-04-2014T15:18:18";
DateTime dtStartDateTime = DateTime.ParseExact(str, "dd-MM-yyyyTHH:mm:ss", CultureInfo.InvariantCulture);
string stime = dtStartDateTime.ToString("yyyy-MM-ddTHH:mm:ss");
This will take the text from the first format and give you the second format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.