c# - ParseExact inverse my day and month -


my code below throw exception invalid date time. error occur after publish server. working find @ developing pc

string str = "27-07-2015 6:15 pm"; datetime dt = convert.todatetime(datetime.parseexact(str, "dd-mm-yyyy h:mm tt", null).tostring("dd-mm-yyyy hh:mm tt")); 

it takes '27' month , '7' day.

what did solve problem:

  • i update datetime format on server dd-mm-yyyy
  • i double checked capital , small letter of date time format.
  • change 'null' 'cultureinfo.invariantculture'
  • change 'pm' 'pm', 'tt' 'tt'
  • read through resources find on google , stackoverflow, nothing's help.

am missing here? know did... :(

as @rawling correctly noted, you're parsing datetime twice: first, using custom formatting, , second, using system's default formatting.

this silly - have datetime parseexact method:

string str = "27-07-2015 6:15 pm"; var dt = datetime.parseexact(str, "dd-mm-yyyy h:mm tt", null); 

that's it, you're done. no need convert string again, , parse once more (and worse, using same custom formatting tostring, subsequent convert.todatetime bound fail anywhere that's not default datetime formatting).


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -