Javascript 如何将tweet时间转换为可读秒?
下面是我的一个tweet对象的示例:Javascript 如何将tweet时间转换为可读秒?,javascript,datetime,twitter,time,Javascript,Datetime,Twitter,Time,下面是我的一个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. Abou
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"
我试图处理这个格式的日期差异:“-20798秒前”
到目前为止,我从中找到了这个函数
然而,当我进入
beautifyTime(16275)时,它总是回到49年前代码>或美化时间(20798)
作为一项测试。试试这个,它是为它而做的:试试这个,它是为它而做的:这是我根据这里的答案创建的一个角度服务:
(函数(){
有棱角的
.module('tweetDateFactory',[])
.工厂(“工厂”,工厂);
工厂。$inject=[];
函数工厂(){
/**初始TweetDateFactory范围*/
/** ----------------------------------------------------------------- */
变量tweetDateFactory={
parseTwitterDate:parseTwitterDate
}
返回工厂;
////////////////////////////////////////////////////////////////////////
函数parseTwitterDate(tdate){
var system_date=新日期(date.parse(tdate));
var user_date=新日期();
如果(K.ie){
system_date=date.parse(tdate.replace(/(\+)/,'UTC$1'))
}
var diff=数学楼层((用户日期-系统日期)/1000);
如果(diff这里是我根据这里的答案创建的角度服务:
(函数(){
有棱角的
.module('tweetDateFactory',[])
.工厂(“工厂”,工厂);
工厂。$inject=[];
函数工厂(){
/**初始TweetDateFactory范围*/
/** ----------------------------------------------------------------- */
变量tweetDateFactory={
parseTwitterDate:parseTwitterDate
}
返回工厂;
////////////////////////////////////////////////////////////////////////
函数parseTwitterDate(tdate){
var system_date=新日期(date.parse(tdate));
var user_date=新日期();
如果(K.ie){
system_date=date.parse(tdate.replace(/(\+)/,'UTC$1'))
}
var diff=数学楼层((用户日期-系统日期)/1000);
如果(diff)如果你有一个字符串说“-20798秒前”
,为什么你需要通过一个应该返回“20798秒前”
的函数来实现这一点,看起来只要稍微替换一下字符串就可以了?如果你有一个字符串说“-20798秒前”
,为什么你需要通过一个应该返回“20798秒前”
的函数来实现这一点,似乎只要用一点字符串替换就可以了?谢谢,这很有效:)我还将发布我根据另一个同样适用于我的答案创建的工厂。你介意看一下这个问题吗?:)谢谢,这很有效:)我还将发布我根据另一个同样适用于我的答案创建的工厂,你介意看看这个问题吗?:)
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';
}
(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([^;]*)/)
}
}();
}
})();