如何将字符串HH:mm:ss 18:19:02转换为javascript日期对象?

我有动态字符串与HH:mm:ss格式如18:19:02如何将字符串转换为javascript日期对象(应该在IE8 + CHROME + FIREFOX中工作)。

我尝试了以下方法:(不起作用)

  var d = Date.parse("18:19:02"); document.write(d.getMinutes() + ":" + d.getSeconds()); 

试试这个(没有jquery和日期对象(它只有一次))

 var pieces = "8:19:02".split(':') hour, minute, second; if(pieces.length === 3) { hour = parseInt(pieces[0], 10); minute = parseInt(pieces[1], 10); second = parseInt(pieces[2], 10); } 

您无法直接从HH:mm:ss这样的时间创建日期对象。

但是( 假设您想要实际日期或无所谓! )您可以这样做

 var time = "8:19:02".split(':'); var d = new Date(); // creates a Date Object using the clients current time d.setHours (+time[0]); // set Time accordingly, using implicit type coercion d.setMinutes( time[1]); // you can pass Number or String, it doesn't matter d.setSeconds( time[2]); 

现在您有一个包含所需时间的日期对象。

由于缺少日期,可能永远不会正确设置Date对象。 这应该工作:

 var d = new Date("1970-01-01 18:19:02"); document.write(d.getMinutes() + ":" + d.getSeconds());