Conditionally convert multiple formats of Date and DateTime string into JavaScript Date Object -
with little utility javascript function below can convert basic date string javascript date object.
the date string can in multiple formats this...
stringtodate("17/9/2014", "dd/mm/yyyy", "/"); stringtodate("9/17/2014", "mm/dd/yyyy", "/"); stringtodate("9-17-2014", "mm-dd-yyyy", "-");
what hoping expand allow conditionally strings have time in them 09:12:49
09:12:49am
, 09:12:49pm
so function below creates date object line...
var formateddate = new date(dateitems[yearindex], month, dateitems[dayindex]);
would have instead like...
var formateddate = new date(dateitems[yearindex], month, dateitems[dayindex], hours, minutes, seconds, milliseconds);
adding hours, minutes, seconds, milliseconds
if detected in original string.
would such task possibble code?
// convert date string js date object // can parse multiple date formats // stringtodate("17/9/2014", "dd/mm/yyyy", "/"); // stringtodate("9/17/2014", "mm/dd/yyyy", "/"); // stringtodate("9-17-2014", "mm-dd-yyyy", "-"); function stringtodate(d, _format, _delimiter) { if((d instanceof date)){ return d; } var formatlowercase = _format.tolowercase(); var formatitems = formatlowercase.split(_delimiter); var dateitems = d.split(_delimiter); var monthindex = formatitems.indexof("mm"); var dayindex = formatitems.indexof("dd"); var yearindex = formatitems.indexof("yyyy"); var month = parseint(dateitems[monthindex]); month -= 1; var formateddate = new date(dateitems[yearindex], month, dateitems[dayindex]); return formateddate; }
i aware of momentjs, datejs, datejs evolved, , sugarjs trying small utility library.
Comments
Post a Comment