People use pretty dates like "20 minutes ago" because they're prettier and
easier for humans. A better solution than using a simple date instead of a
pretty one is to continuously update the prettydate. Something you can
easily accomplish using javascript.
In fact, reddit does it. If you notice the submission date in the comments
keeps being updated. I have implemented a similar solution to reddit for a
web2py application using ractive.
Here is the javascript prettydate function I used.
function pretty_date(time_string) {
var split = time_string.split(' ');
var date = split[0].split('-');
var time = split[1].split(':');
var utc_time_since_epoch = Date.UTC(date[0], date[1] - 1, date[2],
time[0], time[1], time[2]);
var time_formats = [
[60, '{{=T("Just Now")}}'],
[90, '{{=T("1 Minute")}}'], // 60*1.5
[3600, '{{=T("Minutes")}}', 60], // 60*60, 60
[5400, '{{=T("1 Hour")}}'], // 60*60*1.5
[86400, '{{=T("Hours")}}', 3600], // 60*60*24, 60*60
[129600, '{{=T("1 Day")}}'], // 60*60*24*1.5
[604800, '{{=T("Days")}}', 86400], // 60*60*24*7, 60*60*24
[907200, '{{=T("1 Week")}}'], // 60*60*24*7*1.5
[2628000, '{{=T("Weeks")}}', 604800], // 60*60*24*(365/12),
60*60*24*7
[3942000, '{{=T("1 Month")}}'], // 60*60*24*(365/12)*1.5
[31536000, '{{=T("Months")}}', 2628000], // 60*60*24*365,
60*60*24*(365/12)
[47304000, '{{=T("1 Year")}}'], // 60*60*24*365*1.5
[3153600000, '{{=T("Years")}}', 31536000], // 60*60*24*365*100,
60*60*24*365
[4730400000, '{{=T("1 Century")}}'], // 60*60*24*365*100*1.5
];
var seconds = ((new Date).getTime() - utc_time_since_epoch)/1000,
token = ' {{=T("Ago")}}',
i = 0,
format;
while (format = time_formats[i++]) {
if (seconds < format[0]) {
if (format.length == 2) {
return format[1] + (i > 1 ? token : ''); // Conditional so
we don't return Just Now Ago
} else {
return Math.round(seconds / format[2]) + ' ' + format[1] +
(i > 1 ? token : '');
}
}
}
// overflow for centuries
if(seconds > 4730400000)
return Math.round(seconds / 4730400000) + ' {{=T("Centuries")}}' +
token;
}
Notice that it uses web2py templating so make sure to put it in a view (for
instance web2py_ajax.html).
Then you can use setInterval to periodically update the pretty dates.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.