简体   繁体   中英

parse date in mm_dd_yyyy format

I need to parse date in the following format. mm_dd_yyyy I know I can do like this

var dateString = "20050802";
var date = myDate = DateTime.ParseExact(dateString,
                                    "yyyyMMdd",
                                    System.Globalization.CultureInfo.InvariantCulture);

and then replace the -with underscore character. But is there any other way around to do the same?

So, what's the problem?
Just make appropriate format string: MM_dd_yyyy .

var dateTime = DateTime.ParseExact("08_02_2005", "MM_dd_yyyy", CultureInfo.InvariantCulture); // from string to DateTime
var s = dateTime.ToString("MM_dd_yyyy"); // from DateTime to string

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