I'm using django currently and outputting the date in seconds from the unix epoch. How do I use jquery time ago with unix epoch?
我目前正在使用django并从unix时代输出日期。如何在unix时代之前使用jquery?
I see this example: January 10, 2015
我看到这个例子:2015年1月10日
<abbr class="timeago" title="2015-01-10T15:00:00Z">January 10, 2015</abbr>
but can i do something like:
但我可以这样做:
<abbr class="timeago" title="2015-01-10T15:00:00Z">{{UNIX_EPOCH_IN_SECONDS}}</abbr>
Thanks!
4 个解决方案
#1
4
You don't need to convert your unix timestamp to ISO. Hal posted a piece of code modifying jQuery's timeago plugin that worked for me. Simply replace timeago's parse() function at line 89 with this:
您无需将unix时间戳转换为ISO。哈尔发布了一段修改jQuery的timeago插件的代码,对我有用。只需在第89行替换timeago的parse()函数:
parse: function(iso8601) {
if ((iso8601 - 0) == iso8601 && iso8601.length > 0) { // Checks if iso8601 is a unix timestamp
var s = new Date(iso8601);
if (isNaN(s.getTime())) { // Checks if iso8601 is formatted in milliseconds
var s = new Date(iso8601 * 1000); //if not, add milliseconds
}
return s;
}
var s = $.trim(iso8601);
s = s.replace(/-/,"/").replace(/-/,"/");
s = s.replace(/T/," ").replace(/Z/," UTC");
s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
return new Date(s);
},
jQuery Time ago from a timestamp?
jQuery Time from a timestamp?
#2
2
You can initialize a Date
object with a unix timestamp, but Javascript's date expects time in milliseconds, so it's simply:
您可以使用unix时间戳初始化Date对象,但Javascript的日期需要时间(以毫秒为单位),因此它只是:
var d = new Date(<?php echo date('U') ?>000);
which turns into something like:
变成了这样的东西:
var d = new Date(1285027311000);
It'll also parse most standard textual date formats, must as PHP's strtotime() will, though you'll have to test exactly how forgiving it is.
它还将解析大多数标准的文本日期格式,必须像PHP的strtotime()一样,尽管你必须准确测试它是多么宽容。
#3
2
The Date() constructor can take the number of milliseconds since 00:00:00 UTC 1/1/1970, and timeago can be used with a Date object. So:
Date()构造函数可以采用自00/1:00 UTC 1/1/1970以来的毫秒数,timeago可以与Date对象一起使用。所以:
jQuery.timeago(new Date(unixSeconds * 1000));
Should work.
#4
0
Step 1 First add JQuery and timeago jquery plugin
步骤1首先添加JQuery和timeago jquery插件
<script type="text/javascript" src="https://timeago.yarp.com/jquery.timeago.js"></script>
Step 2 No need to write anything in between tags
步骤2无需在标签之间写任何内容
<abbr class="timeago" title="2015-01-10T15:00:00Z"></abbr>
Step 3 Use the following JQuery code
步骤3使用以下JQuery代码
var postAddDate = new Date($(".timeago").attr("title"));
var timeAgo = jQuery.timeago(postAddDate);
$(".timeago").html(timeAgo);
Memory efficient version
内存高效版
$(".timeago").html(jQuery.timeago(new Date($(".timeago").attr("title"))));
见Codepen
#1
4
You don't need to convert your unix timestamp to ISO. Hal posted a piece of code modifying jQuery's timeago plugin that worked for me. Simply replace timeago's parse() function at line 89 with this:
您无需将unix时间戳转换为ISO。哈尔发布了一段修改jQuery的timeago插件的代码,对我有用。只需在第89行替换timeago的parse()函数:
parse: function(iso8601) {
if ((iso8601 - 0) == iso8601 && iso8601.length > 0) { // Checks if iso8601 is a unix timestamp
var s = new Date(iso8601);
if (isNaN(s.getTime())) { // Checks if iso8601 is formatted in milliseconds
var s = new Date(iso8601 * 1000); //if not, add milliseconds
}
return s;
}
var s = $.trim(iso8601);
s = s.replace(/-/,"/").replace(/-/,"/");
s = s.replace(/T/," ").replace(/Z/," UTC");
s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
return new Date(s);
},
jQuery Time ago from a timestamp?
jQuery Time from a timestamp?
#2
2
You can initialize a Date
object with a unix timestamp, but Javascript's date expects time in milliseconds, so it's simply:
您可以使用unix时间戳初始化Date对象,但Javascript的日期需要时间(以毫秒为单位),因此它只是:
var d = new Date(<?php echo date('U') ?>000);
which turns into something like:
变成了这样的东西:
var d = new Date(1285027311000);
It'll also parse most standard textual date formats, must as PHP's strtotime() will, though you'll have to test exactly how forgiving it is.
它还将解析大多数标准的文本日期格式,必须像PHP的strtotime()一样,尽管你必须准确测试它是多么宽容。
#3
2
The Date() constructor can take the number of milliseconds since 00:00:00 UTC 1/1/1970, and timeago can be used with a Date object. So:
Date()构造函数可以采用自00/1:00 UTC 1/1/1970以来的毫秒数,timeago可以与Date对象一起使用。所以:
jQuery.timeago(new Date(unixSeconds * 1000));
Should work.
#4
0
Step 1 First add JQuery and timeago jquery plugin
步骤1首先添加JQuery和timeago jquery插件
<script type="text/javascript" src="https://timeago.yarp.com/jquery.timeago.js"></script>
Step 2 No need to write anything in between tags
步骤2无需在标签之间写任何内容
<abbr class="timeago" title="2015-01-10T15:00:00Z"></abbr>
Step 3 Use the following JQuery code
步骤3使用以下JQuery代码
var postAddDate = new Date($(".timeago").attr("title"));
var timeAgo = jQuery.timeago(postAddDate);
$(".timeago").html(timeAgo);
Memory efficient version
内存高效版
$(".timeago").html(jQuery.timeago(new Date($(".timeago").attr("title"))));
见Codepen