无法使用dd/MM/yyyy HH:mm格式的javascript对日期时间进行排序 [英] Unable to sort date time in javascript with format dd/MM/yyyy HH:mm

查看:51
本文介绍了无法使用dd/MM/yyyy HH:mm格式的javascript对日期时间进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在javascript中按最新日期时间排序.

I am not able to sort by latest date time in javascript.

下面是我的json:

let jsonStr = [{id:1,date:"04/04/2017 17:47"},
               {id:2,date:"05/01/2015 12:35"},
               {id:3,date:"31/02/2017 10:00"},
               {id:4,date:"31/02/2017 12:00"}];

预期结果:最近的日期时间应在列表中排在首位

Expected result : Latest date time should be first in the list

  • 2017年4月4日17:47
  • 2017年2月31日12:00
  • 2017年2月31日10:00
  • 2015年5月1日12:35

当前结果:

  • 2017年2月31日12:00
  • 2017年2月31日10:00
  • 2017年4月4日17:47
  • 2015年5月1日12:35

我使用的排序逻辑:

function sortFunction(a,b){  
   var dateA = new Date(a.date).getTime();
   var dateB = new Date(b.date).getTime();
   return dateB > dateA ? 1 : -1; 
}; 

let jsonStr = [{id:1,date:"04/04/2017 17:47"},
               {id:2,date:"05/01/2015 12:35"},
               {id:3,date:"31/02/2017 10:00"},
               {id:4,date:"31/02/2017 12:00"}];

jsonStr.sort(sortFunction);​

我使用的参考链接:

谢谢.

推荐答案

它不是按照您认为的方式来解释日期.例如,

It's not interpreting the date the way you think it is. For example,

console.log(new Date("05/01/2015 12:35"));

为我制作:

Date 2017-05-01T19:35:00.000Z

表示期望日期为MM/dd/yyyy格式.


您可以推出自己的解决方案,也可以使用其中的一个库.有很多可用的解决方案.我个人喜欢 moment.js ,它允许

which shows that it's expecting the date to be in MM/dd/yyyy format.


You can either roll your own solution or use one of the libraries out there. There are a lot of solutions available. I personally like moment.js which allows date format strings.

另一个示例(引自此答案)为字符串添加了日期解析格式控件:

Another example, quoted from this answer, adds date parsing format control to strings:

String.prototype.toDate = function(format)
{
  var normalized      = this.replace(/[^a-zA-Z0-9]/g, '-');
  var normalizedFormat= format.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
  var formatItems     = normalizedFormat.split('-');
  var dateItems       = normalized.split('-');

  var monthIndex  = formatItems.indexOf("mm");
  var dayIndex    = formatItems.indexOf("dd");
  var yearIndex   = formatItems.indexOf("yyyy");
  var hourIndex     = formatItems.indexOf("hh");
  var minutesIndex  = formatItems.indexOf("ii");
  var secondsIndex  = formatItems.indexOf("ss");

  var today = new Date();

  var year  = yearIndex>-1  ? dateItems[yearIndex]    : today.getFullYear();
  var month = monthIndex>-1 ? dateItems[monthIndex]-1 : today.getMonth()-1;
  var day   = dayIndex>-1   ? dateItems[dayIndex]     : today.getDate();

  var hour    = hourIndex>-1      ? dateItems[hourIndex]    : today.getHours();
  var minute  = minutesIndex>-1   ? dateItems[minutesIndex] : today.getMinutes();
  var second  = secondsIndex>-1   ? dateItems[secondsIndex] : today.getSeconds();

  return new Date(year,month,day,hour,minute,second);
};

示例:

"22/03/2016 14:03:01".toDate("dd/mm/yyyy hh:ii:ss");
"2016-03-29 18:30:00".toDate("yyyy-mm-dd hh:ii:ss");

这篇关于无法使用dd/MM/yyyy HH:mm格式的javascript对日期时间进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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