javascript 正确显示 Instagram 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12612110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Properly displaying Instagram date
提问by tbremer
This is a dirty JSFiddle version of my web app, however, I do not understand how to properly adjust the date from the Unix TimeStamp, all I want to display is the month, day, and year.
这是我的网络应用程序的一个肮脏的 JSFiddle 版本,但是,我不明白如何从 Unix 时间戳正确调整日期,我只想显示月、日和年。
Any help would be greatly appreciated it!
任何帮助将不胜感激!
Here is the Code:
这是代码:
$(function() {
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "https://api.instagram.com/v1/tags/crookedspaces/media/recent/?access_token=16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82",
success: function(data) {
console.log(data);
//OKAY, now lets get to the pretty stuff, INSTAGRAM PEEKTARS.
for (var i = 0; i < 5; i++) {
$(".instagram").append("\
<div class='instagram-feed'>\
<img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px'/>\
<div class='igHover2'>\
posted by: "+data.data[i].user.username+"<br />\
posted on: "+Date(data.data[i].created_time).toString()+"<br />\
</div />\
</div>\
");
}
}
});
});?
回答by Igor Shastin
Here you are:
这个给你:
$(function() {
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "https://api.instagram.com/v1/tags/crookedspaces/media/recent/?access_token=16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82",
success: function(data) {
console.log(data);
//OKAY, now lets get to the pretty stuff, INSTAGRAM PEEKTARS.
for (var i = 0; i < 5; i++) {
var date = new Date(parseInt(data.data[i].created_time) * 1000);
$(".instagram").append("\
<div class='instagram-feed'>\
<img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px'/>\
<div class='igHover2'>\
posted by: "+data.data[i].user.username+"<br />\
posted on: "+(date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear()+"<br />\
</div />\
</div>\
");
date = null;
}
}
});
});
And a live demo with this working http://jsfiddle.net/89YCe/2/
和这个工作http://jsfiddle.net/89YCe/2/的现场演示
回答by Jason
with the previous answer I was getting "NaN" in a lot of cases.
使用之前的答案,我在很多情况下都得到了“NaN”。
Instagram uses a Unix timestamp and I found this script to work the best:
Instagram 使用 Unix 时间戳,我发现这个脚本效果最好:
$(function() {
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "https://api.instagram.com/v1/tags/breathadvisor/media/recent/?access_token=16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82",
success: function(data) {
console.log(data);
//OKAY, now lets get to the pretty stuff, INSTAGRAM PEEKTARS.
for (var i = 0; i < 5; i++) {
var date = new Date(data.data[i].created_time * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = date.getFullYear();
var month = months[date.getMonth()];
var date = date.getDate();
var time = month+', '+ date+' '+year ;
$(".instagram").append("\
<div class='instagram-feed'>\
<img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px'/>\
<div class='igHover2'>\
posted by: "+data.data[i].user.username+"<br />\
posted on: "+time+"<br />\
</div />\
</div>\
");
date = null;
}
}
});
});
});
here's a jsfiddle: http://jsfiddle.net/jgknott/V5TCs/
这是一个 jsfiddle:http: //jsfiddle.net/jgknott/V5TCs/
回答by Sjoerd de Wit
Here's a nice angularjs factory i made for displaying dates exactly like instagram does:
这是我制作的一个很好的 angularjs 工厂,用于像 instagram 一样显示日期:
.factory('displaydate', ['$filter', function($filter) {
return function (date){
// Split timestamp into [ Y, M, D, h, m, s ]
var actiondate = new Date(parseInt(date) * 1000);
var today = new Date();
if(today.getDate() === actiondate.getDate() && today.getMonth() === actiondate.getMonth() && today.getYear() === actiondate.getYear()){
var hourssince = today.getHours() - actiondate.getHours();
var minutessince = today.getMinutes() - actiondate.getMinutes();
var secondssince = today.getSeconds() - actiondate.getSeconds();
if(hourssince > 0){
date = hourssince+'u';
}else if(minutessince > 0){
date = minutessince+'m';
}else{
date = secondssince+'s';
}
}else{
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.round(Math.abs((today.getTime() - actiondate.getTime())/(oneDay)));
if(diffDays >= 7){
date = Math.round(diffDays / 7)+'w';
}else{
if(diffDays == '0'){
diffDays = '1';
}
date = diffDays+'d';
}
}
return date;
};
}])