如何将推特时间转换为可读秒
How to convert tweet time to readable secs?
下面是我的一个tweet对象的示例:
date: "2016-01-12T10:13:50Z"
formatted_date: "January 12, 2016 - 10:13 AM"
formatted_date_difference: "-20798 sec ago"
id: "68358314540"
link: true
literal_text: "One more mention of this: graphic novel night with DEATH theme. About 20 tickets remain. Come!... https://t.co/jInCQ2c8hv"
start_epoch: 1452615230
text: "One more mention of this: graphic novel night with DEATH theme. About 20 tickets remain. Come!... https://t.co/jInCQ2c8hv"
user_name: "watsoncomedian"
我正在尝试使用这个formatted_date_difference: "-20798 sec ago"
到目前为止,我从这个问题中找到了这个函数。
function beautifyTime(timeAgo) {
var seconds = Math.floor((new Date() - timeAgo) / 1000),
intervals = [
Math.floor(seconds / 31536000),
Math.floor(seconds / 2592000),
Math.floor(seconds / 86400),
Math.floor(seconds / 3600),
Math.floor(seconds / 60)
],
times = [
'year',
'month',
'day',
'hour',
'minute'
];
var key;
for(key in intervals) {
if (intervals[key] > 1)
return intervals[key] + ' ' + times[key] + 's ago';
else if (intervals[key] === 1)
return intervals[key] + ' ' + times[key] + ' ago';
}
return Math.floor(seconds) + ' seconds ago';
}
然而,当我进入beautifyTime(16275);
或beautifyTime(20798)
作为测试时,它总是回到49年前。
试试这个,它是为它而生的:瞬间
这是我根据这里的答案创建的一个Angular服务:显示Twitter created_at为xxxx-ago 的JavaScript代码
(function() {
angular
.module('tweetDateFactory', [])
.factory('TweetDateFactory', factory);
factory.$inject = [];
function factory() {
/** Init TweetDateFactory scope */
/** ----------------------------------------------------------------- */
var tweetDateFactory = {
parseTwitterDate : parseTwitterDate
}
return tweetDateFactory;
////////////////////////////////////////////////////////////////////////
function parseTwitterDate(tdate) {
var system_date = new Date(Date.parse(tdate));
var user_date = new Date();
if (K.ie) {
system_date = Date.parse(tdate.replace(/( '+)/, ' UTC$1'))
}
var diff = Math.floor((user_date - system_date) / 1000);
if (diff <= 1) {return "just now";}
if (diff < 20) {return diff + " seconds ago";}
if (diff < 40) {return "half a minute ago";}
if (diff < 60) {return "less than a minute ago";}
if (diff <= 90) {return "one minute ago";}
if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
if (diff <= 5400) {return "1 hour ago";}
if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
if (diff <= 129600) {return "1 day ago";}
if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
if (diff <= 777600) {return "1 week ago";}
return "on " + system_date;
}
// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
var a = navigator.userAgent;
return {
ie: a.match(/MSIE's([^;]*)/)
}
}();
}
})();
相关文章:
- 将日期时间从json转换为可读格式
- 将日期和时间转换为UTC格式的日期-Javascript
- javascript/jquery将utc转换为短格式的本地时间
- 转换异常时间&Datestring到JavaScript日期
- 如何将datetime转换为以毫秒为单位的epoch时间javascript
- 将时间戳转换为java中的ISO860,
- 用JavaScript将ISO时间转换为H:MM am|pm
- 使用select选项转换时间.使用javascript
- 添加css转换时间以使用javascript切换类
- 在 UTC 和特定时区之间转换时间 - 新的国际化 API
- 转换时间中的日期,并使用javascript在其中添加30分钟
- 天气json转换时间Javascript时间戳
- 转换时间戳在UTC到用户本地时间javascript
- 需要使用momentjs转换时间到日期的帮助
- 转换时间字符串格式
- javascript中的For循环转换时间
- 如何转换时间戳并输入<输入类型="日期">
- 转换时间对象
- 如何使用jquery/javascript转换时间/日期
- Javascript转换时间戳