将 Twitter created_at 显示为 xxxx 前的 JavaScript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6549223/
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
JavaScript code to display Twitter created_at as xxxx ago
提问by Scott
I need some JS code that will take the created_at
value from a Twitter feed and display it as xxxx ago.
我需要一些 JS 代码来created_at
从 Twitter 提要中获取值并将其显示为 xxxx 前。
I can find examples of creating the xxxx ago
bit but not examples of getting the created_at
bit into the correct format for JS.
我可以找到创建xxxx ago
位的示例,但找不到将created_at
位转换为 JS 正确格式的示例。
Does anyone have an all in one function to do what I'm after?
有没有人有一个多合一的功能来做我所追求的?
example format Tue Apr 07 22:52:51 +0000 2009
示例格式 Tue Apr 07 22:52:51 +0000 2009
Cannot use new Date(Date.parse("Tue Apr 07 22:52:51 +0000 2009"))
as it gives an invalid date error in IE.
无法使用,new Date(Date.parse("Tue Apr 07 22:52:51 +0000 2009"))
因为它在 IE 中给出了无效的日期错误。
回答by Oncle Tom
回答by Scott
From the comments, and some code from the twitter widget here is the code I came up with:
从评论和来自 twitter 小部件的一些代码这里是我想出的代码:
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'))
}
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([^;]*)/)
}
}();
回答by myselfhimself
Here's a french language translation of Brady's solution:
这是布雷迪解决方案的法语翻译:
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'))
}
var diff = Math.floor((user_date - system_date) / 1000);
if (diff <= 1) {return "à l'instant";}
if (diff < 20) {return "il y a " + diff + " secondes";}
if (diff < 40) {return "il y a une minute";}
if (diff < 60) {return "il y a moins d'une minute";}
if (diff <= 90) {return "il y a une minute";}
if (diff <= 3540) {return "il y a " + Math.round(diff / 60) + " minutes";}
if (diff <= 5400) {return "il y a 1 heure";}
if (diff <= 86400) {return "il y a " + Math.round(diff / 3600) + " heures";}
if (diff <= 129600) {return "il y a 1 jour";}
if (diff < 604800) {return "il y a " + Math.round(diff / 86400) + " jours";}
if (diff <= 777600) {return "il y a 1 semaine";}
return system_date;
}
// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
var a = navigator.userAgent;
return {
ie: a.match(/MSIE\s([^;]*)/)
}
}();
回答by SelimOber
You can use prettyDate
by john Resig. It has also a JQuery plugin.
您可以prettyDate
由 john Resig使用。它还有一个 JQuery 插件。
回答by Oliver Joseph Ash
import * as luxon from 'luxon';
// https://stackoverflow.com/a/20478182/5932012
const TWITTER_DATE_FORMAT = 'EEE MMM d HH:mm:ss ZZZ yyyy';
export const parseTwitterDate = (dateStr: string): luxon.DateTime =>
luxon.DateTime.fromString(dateStr, TWITTER_DATE_FORMAT);
export const formatTwitterDate = (dateTime: luxon.DateTime): string =>
dateTime.toFormat(TWITTER_DATE_FORMAT);
回答by Terrel Jones Jr.
May be hella late but I just found out splitting the string by spaces and creating variables accordingly, easily allows you to breakdown the date.
可能晚了,但我刚刚发现用空格分割字符串并相应地创建变量,很容易让你分解日期。
// Ex. Thu Sep 28 03:40:33 +0000 2017
var tweetDate = data.tweet[i].created_at;
tweetDate = tweetDate.split(' ');
var tweetMo = tweetDate[1];
回答by toutpt
I'm using jquery timeago:
我正在使用 jquery timeago:
var timeago = document.createElement('time')
var dt = new Date(Date.parse(data['status']['created_at']));
var datetimestr = '' + dt.getFullYear() + '-' + ("0" + (dt.getMonth() + 1)).slice(-2) + '-' + dt.getDate() + 'T' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds() + 'Z'
$(timeago).addClass('timeago').attr('datetime', datetimestr).text(data['status']['created_at']);
if(jQuery().timeago) {$(timeago).timeago();}
More on timeago: http://timeago.yarp.com
更多关于 timeago:http://timeago.yarp.com
回答by Jonathan K
I was running into this issue myself, and I think it's time for a grown-up library. I use moment.js for time formatting, so I wrote an extension to do the right display parsing for Twitter:
我自己也遇到了这个问题,我认为是时候建立一个成熟的图书馆了。我使用 moment.js 进行时间格式化,因此我编写了一个扩展来为 Twitter 进行正确的显示解析:
https://github.com/hijonathan/moment.twitter
https://github.com/hijonathan/moment.twitter
It also has a different method for returning super abbreviated timestamps (e.g. 7h
) like in the Twitter iPhone app.
它还有一种不同的方法来返回超级缩写的时间戳(例如7h
),就像在 Twitter iPhone 应用程序中一样。