Reputation: 187
I have method like below to convert datetime to string:
private string GetCurrentDate(DateTime time)
{
time = time.AddSeconds(1);
return $"{time.Year}{time.Month:00}{time.Day}{time.Hour}{time.Minute}{time.Second:00}".Substring(2);
}
the result of Above code is like this :
170902145914
now I want to convert that string to datetime , Iused below code , but it throw exception :
DateTime seed = DateTime.ParseExact($"{20}170902145914","YYYYmmddHHmmss",CultureInfo.CurrentCulture);
how can I do this?
Upvotes: 0
Views: 50
Reputation: 249726
Case matters, lower case for year, upper case M is month, lower case m is minute. upper case H is 24hour hour format.
DateTime seed = DateTime.ParseExact($"20170902145914", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
Check the documentation for more
Upvotes: 3