c# - how to parse datetime according to host datetime format -


i having major issue in project regarding datetime. user gives input date or date database according project requiremnt. format given user different if insert manually , date format database different different servers. bring datetime in 1 format used method tryparseexact() in development runs in production fails production datetime format different parse , takes wrong dateformat instead of taking dd/mm/yyyy takes mm/dd/yyyy. have added code snippet parseing date time.

sqlparameter startingdateparam=null; datetime mdate=null; string idate = geteffectivefromdate();//retriving date database  string[] formats = new[] { "dd-mm-yyyy", "mm/dd/yyyy", "dd/mm/yyyy", "yyyy-mm-dd", "dd/mm/yyyy hh:mm:ss"};  string sysformat = cultureinfo.currentculture.datetimeformat.;//getting host datetime format  if (datetime.tryparseexact(idate, formats, system.globalization.cultureinfo.currentculture, system.globalization.datetimestyles.none, out mdate)) {      startingdateparam = new sqlparameter("@startingdate", sqldbtype.datetime);     startingdateparam.value = mdate; } 

here 1 issue sequence of formats given in formats array, second have add types of datetime formats in array depending upon user input. please unified solution.

your approach impossible!

just imagine user's input of 07/04/2017 (today's date in dd/mm/yyyy). cannot know if 7th of april or 4th of july!

this worse, if users allowed enter year in 2 digits!

and gets still worse, if users allowed type in parts words (e.g. 7 apr 2017.

clear advise: use date-picker control instead of text input. @ least conversion in application , present converted date user. @ moment, when code deals values, validation/conversion has done already...


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -