迄今为止的 Javascript 时间戳 [英] Javascript timestamp to date

查看:24
本文介绍了迄今为止的 Javascript 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我解决这个问题,这让我发疯.我是 Javascript 初学者,现在我第一次遇到日期问题.

Can anybody help me with this, it's driving me crazy. I'm begginer with Javascript, and i encountered problem with dates for the first time now.

我已经问过这个了,但是没有人回答我:json 解析时间或时间戳到 amchartss

I have already asked this, but no one answered me: json with parsed time or timestamp to amchartss

基本上我有这个 XHR 调用.

Basically i have this XHR call.

 getJSON = function(url) {
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, true);
    xhr.responseType = 'json';

    xhr.onload = function() {
        var status = xhr.status;
        if (status == 200) {
            chartLoad(xhr.response);
            console.log(xhr.response);
        } else {
            console.log("oh shit");
        }
    };
    xhr.send();
};

我在这里收到带有时间戳的 JSON 文件的响应,我如何将所有时间戳转换为日期 YYYY-MM-DD HH:mm.

I'm getting in response here JSON file with timestamp, how can i convert all timestamps to date YYYY-MM-DD HH:mm.

之前我直接在服务器上转换日期字符串而不是时间戳,这样我就不需要在客户端进行转换,但这种方式仅适用于 Chrome.

Before instead of timestamps i had date string converted directly on server, in that way i didn't need to do converting on client side, but this way work only on Chrome.

帮助!

推荐答案

虽然 JavaScript 中没有原生 date.format 方法,但您可以为一次性实现开发自己的方法.在你的情况下是这样的:

Although there's no native date.format method in JavaScript you can grow your own for one off implementations. In your case something like:

var newDate = new Date(myTimeStamp);

var outDate = newDate.getFullYear()+"-"+(newDate.getMonth()+1)+"-"+newDate.getDate()+" "+newDate.getHours()+":"+newDate.getMinutes();

今天输出的日期:2014-5-15 16:7注意 getMonth 的 +1 从 0 开始计数如果您想确保值始终为两位数(即单位数前导零),则可能需要额外的摆弄

For todays date that will output: 2014-5-15 16:7 Note the +1 for getMonth which starts counting at 0 Might need an extra bit of fiddling if you want to ensure always two digits on values (ie leading zeros on single digits)

在 xhr onload 处理程序中执行此操作可能是这样的:

To do this within the xhr onload handler might be something like this:

           xhr.onload = function (e) {
                if (this.status == 200) {
                    var blob = this.response;

                    var img = document.createElement('img');
                    img.onload = function (e) {
                        window.URL.revokeObjectURL(img.src); // Clean up after yourself.
                    };
                    img.src = window.URL.createObjectURL(blob);
                    document.body.appendChild(img);

                    var myTimeStamp = e.timeStamp;
                    //I would probably want to put this date code
                    //in a separate function somewhere
                    var newDate = new Date(myTimeStamp);
                    var outDate = newDate.getFullYear()+"-"+(newDate.getMonth()+1)+"-"+newDate.getDate()+" "+newDate.getHours()+":"+newDate.getMinutes();
                    var div = document.createElement('div');
                    div.innerHTML = outDate;
                    document.body.appendChild(div);
                }
            };

这篇关于迄今为止的 Javascript 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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