0

I am trying to convert my string to datetime using ParseExact method but it is not working as expected, Date format in the string is "dd/MM/yyyy" but when i use parseExact method, it changes the format to "MM/dd/yyyy". i want to keep my date format as it is in the string and just want to change string to DateTime. here is my code given below.

string FormattedDate = "18/03/2017";
var parsed = DateTime.ParseExact(FormattedDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

It returns "03/18/2017", how i can keep it same. please help.

Thanks

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Asif Virk
  • 9
  • 1
  • 1
    looks like the problem with how you print the `parsed` date not the problem with `ParseExact`. `03/18/2017` is correct date if your machine culture is `US` – bansi Mar 08 '17 at 02:40
  • yes, my machine culture is US, but i want to display date in Australian culture, how i can do that? please let me know – Asif Virk Mar 08 '17 at 02:50

1 Answers1

4

It is working, since the input string is parsed as DateTime object. You can't change the format of the DateTime object but you can get the value into any format by using format strings.

string oldFormat = parsed.ToString("dd/MM/yyyy");
string anotherFormat = parsed.ToString("yyyy-MMMM-dd");
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • thank you for your reply, how i can display the date in Australian culture, my system culture is US, i just want to change date format of a DateTime varibale to "dd/MM/yyyy". please help when i use string, it returns correct format but when i change it to DateTime, it goes back to "MM/dd/yyyy" – Asif Virk Mar 08 '17 at 02:53
  • don't you tried this `parsed.ToString("dd/MM/yyyy");` to display? – sujith karivelil Mar 08 '17 at 02:55
  • when i use string, it returns correct format but when i change it to DateTime, it goes back to "MM/dd/yyyy". I have to pass that format to DateTime variable not a string. – Asif Virk Mar 08 '17 at 02:56
  • Brother, that is what i said, you can't change the format of the DateTime object you can format them while using(either on displaying or while inserting to database) use will not see that dateTime object anymore – sujith karivelil Mar 08 '17 at 02:58
  • 1
    If you really want to change how you see the datetime format when you debug the application you may need to change the culture of the machine. Or if you want your application to use specific culture, you can change the culture if the main thread of the application. Note: don't depend on the culture of the system as your end user may not be using that culture – bansi Mar 08 '17 at 03:04
  • ok, so how i can change in razor, here is my code @Html.TextBoxFor(model => model.CreatedDate, new { @placeholder = "Register Date"}) – Asif Virk Mar 08 '17 at 03:07
  • This will be an useful threads for formating date in razor[1](http://stackoverflow.com/questions/37524215/how-to-convert-date-time-format-in-a-razor-view), [2](http://stackoverflow.com/questions/28114874/html-displayfor-dateformat-mm-dd-yyyy) – sujith karivelil Mar 08 '17 at 03:10