来自字符串“ HH-MM”形式的Javascript日期对象 [英] Javascript Date Object from string in form 'HH-MM'

查看:72
本文介绍了来自字符串“ HH-MM”形式的Javascript日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,只是想知道如何将 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆