Bra*_*vis 6 html javascript date date-format time-format
我正在使用API从帖子中调用日期.
日期以ISO 8601格式返回:
2015-11-09T10:46:15.097Z
Run Code Online (Sandbox Code Playgroud)
我希望我的日期格式如下:
09/11/2015
Run Code Online (Sandbox Code Playgroud)
然后,我想将它们插入到我的HTML中,如下所示:
$(data).each(function(){
var html = '<randomhtml> '+this.created_at+' <randomhtml>
$('#stream').append(html);
});
Run Code Online (Sandbox Code Playgroud)
有谁知道如何将我的日期更改为正确的格式?
最简单的方法就是使用字符串
$(data).each(function(){
var date = this.created_at.split('T') // split on the "T" -> ["2015-11-09", "10:..."]
.shift() // get the first part -> "2015-11-09"
.split('-') // split again on "-" -> ["2015", "11", "09"]
.reverse() // reverse the array -> ["09", "11", "2015"]
.join('/') // join with "/" -> "09/11/2015"
var html = '<randomhtml> ' + date + ' <randomhtml>';
$('#stream').append(html);
});
Run Code Online (Sandbox Code Playgroud)
因为它是一个UTC日期,只是传递它new Date()会增加时区的差异,而不是总是输出正确的日期.
如果您需要验证日期,则有用于检查有效UTC日期的正则表达式.
小智 6
这可以解决你的问题
var date = new Date('2015-11-09T10:46:15.097Z');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
Run Code Online (Sandbox Code Playgroud)
输出将为“09/11/2015”
| 归档时间: |
|
| 查看次数: |
3356 次 |
| 最近记录: |