[英]Convert full time string to Unix timestamp in jQuery / javascript
I need to produce a timestamp of each date/time string that gets produced in a foreach loop. 我需要为在foreach循环中生成的每个日期/时间字符串生成一个时间戳。
How do I turn this string Mon Aug 08 2016 10:09:42 GMT+0100 (BST)
into a Unix Timestamp for comparison? 我如何将这个字符串
Mon Aug 08 2016 10:09:42 GMT+0100 (BST)
转换为Unix时间戳进行比较?
I am then going to use that single value to do a jQuery sort (code below) 然后,我将使用该单个值进行jQuery排序(下面的代码)
var boards = $(".socialBox");
boards.sort(function(a, b){
return $(a).data("date") - $(b).data("date");
});
$("#social-board").html(boards);
As you can imagine the above code doesn't work on the current date/time string. 您可以想象上面的代码不适用于当前日期/时间字符串。
You can try this: 您可以尝试以下方法:
new Date('Mon Aug 08 2016 10:09:42 GMT+0100 (BST)').getTime();
The getTime()
function returns the number of milliseconds since 1970/01/01 getTime()
函数返回自1970/01/01以来的毫秒数
You can convert the string data to date object along with .getTime()
to get number of milliseconds since 1970/01/01: 您可以将字符串数据与
.getTime()
一起转换为date对象,以获取自1970/01/01以来的毫秒数:
boards.sort(function(a, b){
return new Date($(a).data("date")).getTime() - new Date($(b).data("date")).getTime();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.