How can I manipulate dates so that they show up as "just now"... "5 mins ago"... "3 hours ago"... "June 22nd, 2010 at 1:45pm" in similar fashion to how SO displays the dates next to the answers/comments to each question?
我如何操纵日期,以便它们显示为“刚才”...“5分钟前”...“3小时前”...“2010年6月22日下午1:45”以类似的方式如何SO显示每个问题的答案/评论旁边的日期?
To further complicate matters, the dates stored in my database are in GMT time (which is fine), but I want them to appear in the timezone of each user's browser.
更复杂的是,我的数据库中存储的日期是GMT时间(很好),但我希望它们出现在每个用户浏览器的时区中。
I've already tried John Resig's pretty date plugin: http://bassistance.de/jquery-plugins/jquery-plugin-prettydate/, and I've edited it so that it subtracts the timezone offset from the GMT time in the database. However, this solution only works in FireFox.
我已经尝试了John Resig的漂亮日期插件:http://bassistance.de/jquery-plugins/jquery-plugin-prettydate/,我编辑了它,以便从数据库中的GMT时间中减去时区偏移量。但是,此解决方案仅适用于FireFox。
Here's the 'prettydate' function after I've added the timezone offset:
添加时区偏移后,这是'prettydate'函数:
format : function(time) {
var date = new Date(time);
var currentDate = new Date();
var timezoneOffsetInMilliseconds = currentDate.getTimezoneOffset() * 60000;
var currentTimeInMillisecondsUtc = currentDate.getTime();
var givenTimeInMillisecondsUtc = date.getTime()- timezoneOffsetInMilliseconds;
var diffInSeconds = ((currentTimeInMillisecondsUtc - givenTimeInMillisecondsUtc) / 1000);
var day_diff = Math.floor(diffInSeconds / 86400);
if (isNaN(day_diff) || day_diff < 0)
return;
// If longer than a month, calculate the date w/ timezone offset
if (day_diff >= 31)
return new Date(givenTimeInMillisecondsUtc).toLocaleString();
var messages = $.prettyDate.messages;
return day_diff == 0 && (diffInSeconds < 60 && messages.now
|| diffInSeconds < 120 && messages.minute
|| diffInSeconds < 3600
&& messages.minutes(Math.floor(diffInSeconds / 60))
|| diffInSeconds < 7200 && messages.hour || diffInSeconds < 86400
&& messages.hours(Math.floor(diffInSeconds / 3600)))
|| day_diff == 1 && messages.yesterday || day_diff < 7
&& messages.days(day_diff) || day_diff < 31
&& messages.weeks(Math.ceil(day_diff / 7));
}
Edit: I'm using Python with Django templates (via Google Webapp), and the 'time' object I'm passing in a 'db.DateTimeProperty()' in the iso format like so:
编辑:我正在使用带有Django模板的Python(通过Google Webapp),以及我以iso格式传递'db.DateTimeProperty()'的'time'对象,如下所示:
<span class="prettyDate" title='{{ q.date.isoformat }}'>{{q.date.isoformat}}</span>
1 个解决方案
#1
2
Why not do this server-side, with the built-in template tag timesince, or a custom template tag?
为什么不使用内置模板标记timesince或自定义模板标记来执行此服务器端?
In relative time, there is not meaning to timezones, as long the 2 times you are diffing are in the same zone. For the results that are farther away in the past, and you want to present as absolute times, you'll have to do the time shift yourself. for this, you'll have to ask the user for her timezone (or use some JS on a previous page to send it to you) and store it in the user profile.
在相对时间内,对于时区没有意义,只要您正在进行的2次传播属于同一区域。对于过去距离较远的结果,并且您希望以绝对时间呈现,您将不得不自己做时间转移。为此,您必须向用户询问她的时区(或使用前一页上的某些JS将其发送给您)并将其存储在用户配置文件中。
this is also discussed in the mailing list.
这也在邮件列表中讨论。
#1
2
Why not do this server-side, with the built-in template tag timesince, or a custom template tag?
为什么不使用内置模板标记timesince或自定义模板标记来执行此服务器端?
In relative time, there is not meaning to timezones, as long the 2 times you are diffing are in the same zone. For the results that are farther away in the past, and you want to present as absolute times, you'll have to do the time shift yourself. for this, you'll have to ask the user for her timezone (or use some JS on a previous page to send it to you) and store it in the user profile.
在相对时间内,对于时区没有意义,只要您正在进行的2次传播属于同一区域。对于过去距离较远的结果,并且您希望以绝对时间呈现,您将不得不自己做时间转移。为此,您必须向用户询问她的时区(或使用前一页上的某些JS将其发送给您)并将其存储在用户配置文件中。
this is also discussed in the mailing list.
这也在邮件列表中讨论。