我需要一些JS代码,它会从Twitter提要中获取created_at
值并将其显示为xxxx之前。
我可以找到创建xxxx ago
位的示例,但不能找到将created_at
位转换为JS的正确格式的示例。
是否有人拥有一体化功能来完成我所追求的目标?
示例格式Tue Apr 07 22:52:51 +0000 2009
无法使用new Date(Date.parse("Tue Apr 07 22:52:51 +0000 2009"))
因为它在IE中提供了无效的日期错误。
答案 0 :(得分:43)
在没有任何插件的情况下使用moment.js这是您需要用来正确解析尴尬的Twitter日期的自定义格式:
var tweetDate = 'Mon Dec 02 23:45:49 +0000 2013';
moment(tweetDate, 'dd MMM DD HH:mm:ss ZZ YYYY', 'en');
答案 1 :(得分:28)
从评论中,这里有来自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$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([^;]*)/)
}
}();
答案 2 :(得分:5)
这是Brady解决方案的法语翻译:
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 "à 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([^;]*)/)
}
}();
答案 3 :(得分:3)
您可以使用john Resig的prettyDate
。它还有一个JQuery插件。
答案 4 :(得分:2)
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);
答案 5 :(得分:0)
我正在使用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();}
更多关于timeago:http://timeago.yarp.com
答案 6 :(得分:0)
我自己也遇到过这个问题,我认为现在是成熟图书馆的时候了。我使用moment.js进行时间格式化,所以我写了一个扩展来为Twitter做正确的显示解析:
https://github.com/hijonathan/moment.twitter
它还有一种不同的方法可以返回超级缩写时间戳(例如7h
),就像在Twitter iPhone应用程序中一样。
答案 7 :(得分:0)
可能会很晚,但我发现用空格分割字符串并相应地创建变量,很容易让你分解日期。
// Ex. Thu Sep 28 03:40:33 +0000 2017
var tweetDate = data.tweet[i].created_at;
tweetDate = tweetDate.split(' ');
var tweetMo = tweetDate[1];