javascript - parsing user input to date -
i have form users insert date (edit: input copied form documents "as is" formatting vary)
"2012 12 01"
"2012-12-01"
"2012.12.01"
"01.15.2012"
also not friendly (but fast typing!) inputs like:
"01122012" // 01 12 2012
"011212" // 01 12 2012
the input format not fixed should create bast out of ...
of course of study there priority:
"12.12.12" should parsed yy.mm.dd if valid, or dd.mm.yy sec option.
most of ready functions work "properly formated" content .. need alghorithm (or illustration code parsers in other lanuages)
you "straight forward" like:
var datestring; // user input date if(datestring.split(" ").length == 3){ // have "yyyy mm dd" format } else if(datestring.split("-").length == 3){ // have yyyy-mm-dd format } else if(datestring.split(".").length == 3){ // have "yyyy.mm.dd" or "mm.dd.yyyy" format } else { // suppose have "yyyymmdd" or "mmddyyyy" format }
in cases can have 2 different formats have check day, year , month values if valid year. if so, can build date, otherwise seek other way.
for yyyymmdd , mmddyyyy combinations can utilize substr()
function on string extract day, year , month.
however, have take business relationship user input can wrong "12 march 2012" or "abcdefg", wrap whole functionality try-catch block.
javascript
No comments:
Post a Comment