I'm using timepicker and it requires a date object. From database I'm getting a time string like "17:00:00". How can I convert a time string like "17:00:00" into date object?
Edit I have already tried the solution in question suggested by Mike C, Alex K but in that question they are converting a date string into a date object and when I try to convert time string into date I get an invalid date error.
var a = "17:00"
var b = toDate(a,"h:m")
alert(b);
function toDate(dStr,format) {
var now = new Date();
if (format == "h:m") {
now.setHours(dStr.substr(0,dStr.indexOf(":")));
now.setMinutes(dStr.substr(dStr.indexOf(":")+1));
now.setSeconds(0);
return now;
}else
return "Invalid Format";
}
To work with dates you can write your own parser or try already proven libraries like http://momentjs.com (what I would suggest to do).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With