使用javascript如何排序日期和时间戳(日期格式是这样的(例如:09 / may / 2014 13:45:21)24小时格式的时间戳 [英] using javascript how to sort date and timestamp (format of date is like this(ex:09/may/2014 13:45:21) timestamp in 24 hours format

查看:487
本文介绍了使用javascript如何排序日期和时间戳(日期格式是这样的(例如:09 / may / 2014 13:45:21)24小时格式的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用javascript如何排序日期和时间戳(日期的格式是这样的(例如:09 / may / 2014 13:45:21)24小时格式的时间戳请帮助我这个

解决方案

您好,

如果所有日期都是数组的一部分,那么可以通过以下代码轻松对它们进行排序。



  //  让我们说,你有一个数组中的所有日期 
var dates = [
' 09 / may / 2014 13:45:21'' 31 / dec / 2012 13:45:21'' 09 / may / 2014 1:46:21'' 09 / may / 2013 22:45:21'' 12 / jan / 2014 03:00:21']

// 然后可以将sort函数传递给array.sort()
var sortedDates = dates.sort( function (var1,var2){
var a = new 日期(var1),b = < span class =code-keyword> new Date (var2);
if (a> b)
return 1 ;
if (a< b)
return -1;

return 0 ;
});
// /现在'sortedDates'包含按升序排列的日期。


using javascript how to sort date and timestamp (format of date is like this(ex:09/may/2014 13:45:21) timestamp in 24 hours format kindly help me on this

解决方案

Hi,
If all the dates are part of an array then they can be easily sorted by the below code.

//Lets say , you have all your dates in an array
var dates = [
  '09/may/2014 13:45:21' ,'31/dec/2012 13:45:21' , '09/may/2014 1:46:21' , '09/may/2013 22:45:21' ,'12/jan/2014 03:00:21' ]
  
//Then a sort function can be passed to the array.sort()
var sortedDates = dates.sort(function (var1, var2) { 
   var a= new Date(var1), b = new Date(var2);
    if (a > b)
      return 1;
    if (a < b)
      return -1;
   
    return 0;
});
/// Now the 'sortedDates' contain the dates in ascending order.


这篇关于使用javascript如何排序日期和时间戳(日期格式是这样的(例如:09 / may / 2014 13:45:21)24小时格式的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆