简体   繁体   中英

convert string to datetime according to Culture

does any one know how to convert string into date according to user's culture

i am using this code

DateTime.Parse("2-7-1997", CultureInfo.InvariantCulture); 

it works fine since my culture is set to us

but if i pass

DateTime.Parse("23-7-1997", CultureInfo.InvariantCulture);

it throws Format exception

String was not recognized as a valid DateTime.

Is there any thing which converts string into date according to user culture

// convert it to a datetime
// "d" is the required format
var date = DateTime.ParseExact("7/23/1997", "d", CultureInfo.InvariantCulture);

// now you can output the date in the user's culture
var localizedDateString = date.ToString("d");

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