簡體   English   中英

將字符串轉換為Date對象並在js中進行比較

[英]convert string to Date object and compare in js

如何比較這種格式的兩個日期

     var CurrDate = new Date().format("MM/dd/yyyy");
     if (Date.parse("05-Jun-2012")>Date.parse(CurrDate))
        {
         alert("Please enter future date!");
         return false;
        }

請幫助確認日期。

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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM