如何使用javascript或jquery将时间转换为javascript时间戳

How to convert a time to javascript-timestamps using javascript or jquery?

本文关键字:javascript 转换 时间 时间戳 jquery 何使用      更新时间:2023-09-26

如何将此时间11:34转换为javascript时间戳。任何可用的javascript功能。

我正在尝试创建一个浮动图,我正在使用Flot库。在我的图表中,时间在x轴上,计数在y轴上。为了创建数据部分,我需要将时间转换为时间戳,就像API文档中指定的那样。

http://people.iola.dk/olau/flot/API.txt

这是我的代码

var datasets = {
    "usa": {
        label: "Logged Users",
        data: [[10:55, 4], [11:00, 1], [11:05, 4], [11:10, 2], [11:15, 3], [11:20, 1], [11:25, 5]]
    }
};
if (datasets.length > 0){
    $.plot($("#placeholder"), datasets, {
        yaxis: { min: 0,tickDecimals: 0 },
        xaxis: {  mode: "time",timeformat: "%H:%M" }
    });
}

它不起作用,因为我指定了确切的时间而不是数字。所以我需要将其转换为时间戳格式
请帮帮我。

感谢

使用Date对象的实例:

var sTime = '11:34';
var oDate = new Date();
oDate.setUTCHours(
    parseInt(sTime.substr(0, 2), 10),
    parseInt(sTime.substr(3, 2), 10),
    0,
    0
);
var sTimestamp = oDate.getTime();

另请参见此示例。

===更新===

当时间是本地时间而不是UTC时,您可以使用设置时间

oDate.setHours(
    parseInt(sTime.substr(0, 2), 10),
    parseInt(sTime.substr(3, 2), 10),
    0,
    0
);

另请参见此示例。

p.s.:getTime()的结果以毫秒为单位。

===更新===

要映射当前数据集,您可以使用以下脚本(使用UTC;如果您希望本地时间在setter中删除UTC):

var aCountries = [ "usa" ];
var oDate = new Date();
oDate.setSeconds(0, 0);
for (var i = 0; i < aCountries.length; i++) {
    datasets[aCountries[i]].data =
        datasets[aCountries[i]].data.map(function(oElement) {
            oDate.setUTCHours(
                parseInt(oElement[0].substr(0, 2), 10),
                parseInt(oElement[0].substr(3, 2), 10)
            );
            return [
                oDate.getTime()
                , oElement[1]
            ];
        });
}

另请参见此示例。

    var now = new Date();
    now.format("m/dd/yy");
// Returns, e.g., 6/09/07
// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21
// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can''t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!
// When using the standalone dateFormat function,
// you can also provide the date as a string
dateFormat("Jun 9 2007", "fullDate");
// Saturday, June 9, 2007
// Note that if you don't include the mask argument,
// dateFormat.masks.default is used
now.format();
// Sat Jun 09 2007 17:46:21
// And if you don't include the date argument,
// the current date and time is used
dateFormat();
// Sat Jun 09 2007 17:46:22
// You can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
dateFormat("longTime");
// 5:46:22 PM EST
// And finally, you can convert local time to UTC time. Either pass in
// true as an additional argument (no argument skipping allowed in this case):
dateFormat(now, "longTime", true);
now.format("longTime", true);
// Both lines return, e.g., 10:46:21 PM UTC
// ...Or add the prefix "UTC:" to your mask.
now.format("UTC:h:MM:ss TT Z");
// 10:46:21 PM UTC
Use all date time example .,so use it.

我发现jQuery全球化插件的日期解析效果最好。其他方法存在跨浏览器问题,像date.js这样的东西已经有很长一段时间没有更新了。

页面上也不需要日期选择器。你可以调用类似于文档中给出的例子:

$.parseDate('yy-mm-dd', '2007-01-26');