来自字符串“ HH-MM”形式的Javascript日期对象 [英] Javascript Date Object from string in form 'HH-MM'
问题描述
嘿,只是想知道如何将 HH:MM
字符串转换为javascript Date
对象。我试过 new Date(string);
和 myDate.setTime()
,但无济于事。
Hey, just wondering how to convert an HH:MM
string into a javascript Date
object. I have tried new Date(string);
and myDate.setTime()
but to no avail.
另一个问题可能是:如何将 HH:MM
中的字符串转换为从1970年1月1日起的毫秒数。
A side question could be: How to convert a string in HH:MM
into milliseconds from Jan 1, 1970.
谢谢您的帮助。
推荐答案
在JavaScript中,使用datejs库。 http://www.datejs.com/
如果包含此库,则可以使用称为 parseExact,您可以像这样使用它:
in JavaScript, I'm using the datejs library. http://www.datejs.com/ If you include this library, you have a function called "parseExact" and you could use it like this:
var dateString = "10-12";
var date = new Date.parseExact(dateString, "hh-mm");
要获取毫秒数,可以从 http://code.google.com/p/datejs/source/browse/trunk/# trunk / src 。然后,您可以使用函数getTotalMilliseconds():
To get the miliseconds, you can download the file time.js from http://code.google.com/p/datejs/source/browse/trunk/#trunk/src. Then you have a function getTotalMilliseconds() you can use:
var mSeconds = date.getTotalMilliseconds();
我希望这会有所帮助。
这篇关于来自字符串“ HH-MM”形式的Javascript日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!