- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能帮我解决这个问题,这让我发疯。我是 Javascript 的初学者,我现在第一次遇到日期问题。
我已经问过了,但是没有人回答我: json with parsed time or timestamp to amchartss
基本上我有这个 XHR 调用。
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。
之前我直接在服务器上转换日期字符串而不是时间戳,这样我不需要在客户端进行转换,但这种方式只适用于 Chrome。
帮助!
最佳答案
尽管 JavaScript 中没有原生的 date.format 方法,但您可以自行开发一次性实现。在你的情况下是这样的:
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 开始计数如果你想确保值总是两位数(即一位数前导零),可能需要额外的一点摆弄
在 xhr 加载处理程序中执行此操作可能是这样的:
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 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23681273/
给定一个带有多个 date_time 戳的字符串,我想 提取第一个戳及其前面的文本 候选字符串可以有一个或多个时间戳 后续的 date_time 戳记将被 sep="-" 隔开 后续date_time
是否可以合并从相机拍摄的文本和照片?我想在照片上标记日期和时间,但我在 Google 上找不到任何内容。 最佳答案 使用下面的代码来实现你所需要的。 Bitmap src = Bitm
有没有办法通过 Graph API 戳另一个用户?基于this post ,并使用 Graph Explorer ,我发布到“/USERID/pokes”,我已经授予它(Graph API 应用程序和
我有两个向左浮动的元素。一个是 body 的第一个 child ,另一个是容器的第一个 child ,容器是 body 的第二个 child 。 ...
我是一名优秀的程序员,十分优秀!