简体   繁体   中英

how to Parse string to DateTime with string number

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?

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM