验证此“dd-MMM-yyyy”的两个日期javascript中的格式 [英] Validate two dates of this "dd-MMM-yyyy" format in javascript

查看:100
本文介绍了验证此“dd-MMM-yyyy”的两个日期javascript中的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个格式的两个日期 2010年8月18日 2010年8月19日

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

推荐答案

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

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

这篇关于验证此“dd-MMM-yyyy”的两个日期javascript中的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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