简体   繁体   中英

Convert from a string to datetime

First of all I have a xamdatetimeeditor which is for user to select the date and time. It is in the format of "yyyy/MM/dd HH:mm:ss". From there, I would like to convert it to "yyyyMMddHHmmss" format. Which means just removing the special characters and spaces.

Do I have to use the DateTime.TryParseExact method? Or do I have to use the string replace function? What is the best solution?

FOUND THE SOLUTION!! just have to use string format = "yyyyMMddHHmmss"; and date = getdate.ToString(format);

Try this:

var input = "2004/01/02 12:01:03";
DateTime aDate;
DateTime.TryParse(input, out aDate);
string output = aDate.ToString("yyyyMMddHHmmss");

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