0

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

justderb
  • 2,835
  • 1
  • 25
  • 38
John
  • 235
  • 1
  • 6
  • 17
  • What defines "to user's culture" ?? Is this a windows app, a web app ??? – Steen Tøttrup Jul 31 '12 at 05:08
  • windows app, user cuture means Culture setting like for date currency etc – John Jul 31 '12 at 05:09
  • 3
    Refer this link http://stackoverflow.com/questions/1368636/why-cant-datetime-parseexact-parse-9-1-2009-using-m-d-yyyy – Lajja Thaker Jul 31 '12 at 05:10
  • Your question is bit confusing, do you want to parse a string and output as in current culture ? – V4Vendetta Jul 31 '12 at 05:11
  • Well, if it's a windows app, you should be able to just convert the datetime, the user's culture/regional settings should ensure that you're converting the string to a date. On my computer your app would convert 23/7/2012 correctly, because I've selected Danish for my regional settings. – Steen Tøttrup Jul 31 '12 at 05:19
  • @LajjaThaker the link work for me – John Jul 31 '12 at 05:21

1 Answers1

2
// 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");
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105