Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting time string into Date object [duplicate]

Tags:

javascript

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.

like image 639
sqlcte Avatar asked Oct 16 '25 10:10

sqlcte


2 Answers

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";
}
like image 185
Kurenai Kunai Avatar answered Oct 18 '25 23:10

Kurenai Kunai


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).

like image 23
Vitalii Petrychuk Avatar answered Oct 18 '25 23:10

Vitalii Petrychuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!