datetime - TryParseExact doesn't receive valid format c# -


i trying input datetime value console. however, tryparseexact method doesn't receive valid formats:

string startdate; datetime inputdate; while (true) {    console.writeline("input time [hh:mm tt]");    startdate = console.readline();    if (datetime.tryparseexact(startdate, "hh:mm tt",                               cultureinfo.currentculture,                               datetimestyles.none, out inputdate))    {       break;    } } 

any suggestions?

if not accepting input think valid there 2 possible causes of problem:

1) input not think is. caused copying , pasting somewhere else including non-valid characters or something. if manually typing number unlikely issue.

2) format accepting not think is. since there subtleties can bite on this. key think in http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx , aware several elements using culture dependant. @ least partially aware of cultures since explicitly stating want current culture. without knowing culture though hard input should be.

a case in point in format : doesn't mean literal colon "time separator". quote msdn page: "the ":" custom format specifier represents time separator, used differentiate hours, minutes, , seconds. appropriate localized time separator retrieved datetimeformatinfo.timeseparator property of current or specified culture."

as can see means not :.

often best fix, since hard definining format, use cultureinfo.invariantculture guarantee not change depending on run software, etc. otherwise should generate string specifies correct input using relevant components of current culture object.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -