John
John

Reputation: 235

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

Upvotes: 0

Views: 1977

Answers (1)

Vishal Suthar
Vishal Suthar

Reputation: 17194

// 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");

Upvotes: 2

Related Questions