【问题标题】:How to convert a time to javascript-timestamps using javascript or jquery?如何使用 javascript 或 jquery 将时间转换为 javascript-timestamps?
【发布时间】:2012-03-12 13:03:52
【问题描述】:

我将如何将这个时间 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" }
    });
}

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

谢谢

【问题讨论】:

  • 它是记录事件的时间戳还是什么,更多地描述你想要什么?

标签: javascript jquery time timestamp


【解决方案1】:

使用Date object的实例:

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();

另见this example

=== 更新 ===

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

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

另见this example

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

=== 更新 ===

要映射您当前的数据集,您可以使用以下脚本(使用 UTC;如果您想要本地时间,请在设置器中删除 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]
            ];
        });
}

另见this example

【讨论】:

  • 请注意,时间戳总是包含“日期”部分(即当天)。除非您创建自己的类型,否则无法在 JavaScript 中创建“仅小时”时间戳。
【解决方案2】:
    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.

【讨论】:

    【解决方案3】:

    我发现jQuery Globalization Plugin 日期解析效果最好。其他方法存在跨浏览器问题,并且 date.js 之类的东西已经有一段时间没有更新了。

    您也不需要页面上的 datePicker。您可以调用类似于文档中给出的示例的内容:

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

    【讨论】:

      猜你喜欢
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多