【发布时间】:2011-04-15 10:25:49
【问题描述】:
我渲染这个 JSON 对象:
[{"created_at":"2010-09-21T20:41:28Z","subject":"hello world"}]
然后我使用这个日期解析器来解析它(见下文),但它只适用于 Chrome 6.0.4、Firefox 3.6.8,但不适用于 Safari 5.0.2 --- 我收到 NaN 错误。什么给了?
Date.prototype.toRelativeTime = function(now_threshold) {
var delta = new Date() - this;
now_threshold = parseInt(now_threshold, 10);
if (isNaN(now_threshold)) {
now_threshold = 0;
}
if (delta <= now_threshold) {
return 'Just now';
}
var units = null;
var conversions = {
millisecond: 1, // ms -> ms
second: 1000, // ms -> sec
minute: 60, // sec -> min
hour: 60, // min -> hour
day: 24, // hour -> day
month: 30, // day -> month (roughly)
year: 12 // month -> year
};
for (var key in conversions) {
if (delta < conversions[key]) {
break;
} else {
units = key; // keeps track of the selected key over the iteration
delta = delta / conversions[key];
}
}
// pluralize a unit when the difference is greater than 1.
delta = Math.floor(delta);
if (delta !== 1) { units += "s"; }
return [delta, units, "ago"].join(" ");
};
/*
* Wraps up a common pattern used with this plugin whereby you take a String
* representation of a Date, and want back a date object.
*/
Date.fromString = function(str) {
return new Date(Date.parse(str));
};
【问题讨论】:
标签: javascript jquery ruby-on-rails json datetime