javascript 在javascript中验证这种“dd-MMM-yyyy”格式的两个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3509683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:22:35  来源:igfitidea点击:

Validate two dates of this "dd-MMM-yyyy" format in javascript

javascriptvalidationdate

提问by ACP

I have two dates 18-Aug-2010and 19-Aug-2010of this format. How to find whether which date is greater?

我有两个日期18-Aug-201019-Aug-2010这种格式。如何找到哪个日期更大?

回答by CMS

You will need to create a custom parsing function to handle the format you want, and get date objects to compare, for example:

您将需要创建一个自定义解析函数来处理您想要的格式,并获取要比较的日期对象,例如:

function customParse(str) {
  var months = ['Jan','Feb','Mar','Apr','May','Jun',
                'Jul','Aug','Sep','Oct','Nov','Dec'],
      n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;

  while(n--) { months[months[n]]=n; } // map month names to their index :)

  matches = str.match(re); // extract date parts from string

  return new Date(matches[3], months[matches[2]], matches[1]);
}

customParse("18-Aug-2010");
// "Wed Aug 18 2010 00:00:00"

customParse("19-Aug-2010") > customParse("18-Aug-2010");
// true

回答by naikus

You can do the parsing manually, for your given format, but I'd suggest you use the date.jslibrary to parse the dates to Date objects and then compare. Check it out, its awesome!

您可以针对给定的格式手动进行解析,但我建议您使用date.js库将日期解析为 Date 对象,然后进行比较。看看,太棒了!

And moreover, its a great addition to your js utility toolbox.

此外,它是您的 js 实用工具箱的一个很好的补充。

回答by matyr

The native Datecan parse "MMM+ dd yyyy", which gives:

本机Date可以解析“MMM+ dd yyyy”,它给出:

function parseDMY(s){
  return new Date(s.replace(/^(\d+)\W+(\w+)\W+/, '  '));
}
+parseDMY('19-August-2010') == +new Date(2010, 7, 19) // true
parseDMY('18-Aug-2010') < parseDMY('19-Aug-2010')     // true

回答by Delan Azabani

Firstly, the 'dd-MMM-yyyy' format isn't an accepted input format of the Dateconstructor (it returns an "invalid date" object) so we need to parse this ourselves. Let's write a function to return a Dateobject from a string in this format.

首先,'dd-MMM-yyyy' 格式不是Date构造函数可接受的输入格式(它返回一个“无效日期”对象),所以我们需要自己解析它。让我们编写一个函数来Date从这种格式的字符串中返回一个对象。

function parseMyDate(s) {
    var m = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
    var match = s.match(/(\d+)-([^.]+)-(\d+)/);
    var date = match[1];
    var monthText = match[2];
    var year = match[3];
    var month = m.indexOf(monthText.toLowerCase());
    return new Date(year, month, date);
}

Dateobjects implicitly typecast to a number (milliseconds since 1970; epoch time) so you can compare using normal comparison operators:

Date对象隐式转换为数字(自 1970 年以来的毫秒数;纪元时间),因此您可以使用普通比较运算符进行比较:

if (parseMyDate(date1) > parseMyDate(date2)) ...

回答by mplungjan

Update: IE10, FX30 (and likely more) will understand "18 Aug 2010" without the dashes - Chrome handles either

更新:IE10、FX30(可能还有更多)会在没有破折号的情况下理解“18 Aug 2010”——Chrome 也可以处理

so Date.parse("18-Aug-2010".replace("/-/g," "))works in these browsers (and more)

所以Date.parse("18-Aug-2010".replace("/-/g," "))适用于这些浏览器(以及更多)

Live Demo

Live Demo

Hence

因此

function compareDates(str1,str2) {
  var d1 = Date.parse(str1.replace("/-/g," ")),
      d2 = Date.parse(str2.replace("/-/g," "));
  return d1<d2;
}