简体   繁体   中英

Convert ShortTime string to DateTime format

DateTime time = DateTime.Now;

string newTime = time.ToShortTime();

How to convert this newTime string into DateTime format

Please try with the below code snippet.

DateTime time = DateTime.Now;

string newTime = time.ToShortTimeString();

DateTime dt = new DateTime();
DateTime.TryParse(newTime,out dt);

Note : It will set current Date.

Finaly I found an answer

const string FMT = "O";
DateTime now1 = DateTime.Now;
string strDate = now1.ToString(FMT);
DateTime now2 = DateTime.ParseExact(strDate, FMT, CultureInfo.InvariantCulture);

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