jQuery 比较jquery中的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5000693/
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-08-26 18:21:56  来源:igfitidea点击:

Comparing dates in jquery

jquerydate-comparison

提问by learning

I am having the following codes and it though i'm having 01-Jan-2009for DateToand 03-Jan-2009for DateFromit's reading the values as NAN. Am I missing anything? I`m referencing

我有以下代码,尽管我有01-Jan-2009forDateTo并且03-Jan-2009forDateFrom它正在将值读取为NAN. 我错过了什么吗?我参考

 var DateToValue = $("#DateTo").val();
      var DateFromValue = $("#DateFrom").val();

      if (Date.parse(DateToValue) <= Date.parse(DateFromValue)) {
          $("#DateFrom").val(DateToValue)
      }
  <script src="@Url.Content("~/Scripts/jquery-1.4.2.min.js")"
 type="text/javascript"></script>

 <script  src="@Url.Content("~/Scripts/jquery.datePicker.js")"
type="text/javascript"></script>

         <script src="@Url.Content("~/Scripts/jquery.validate.js")"
 type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/jquery-1.4.2.min.js")"
 type="text/javascript"></script>

 <script  src="@Url.Content("~/Scripts/jquery.datePicker.js")"
type="text/javascript"></script>

         <script src="@Url.Content("~/Scripts/jquery.validate.js")"
 type="text/javascript"></script>

采纳答案by chriszero

Use DateJS for parsing your date. http://www.datejs.com/just include the script in your html.

使用 DateJS 解析您的日期。http://www.datejs.com/只需将脚本包含在您的 html 中。

回答by Johnny DropTables

How about this?

这个怎么样?

  DateTime DateToValue = $("#DateTo").val();
  DateTime DateFromValue = $("#DateFrom").val();

  if (Date.parse(DateToValue) <= Date.parse(DateFromValue)) {
      $("#DateFrom").val(DateToValue)
  }

回答by Vishal Sharma

The Easy Way to Do is

最简单的方法是

 var start= new Date($('#txtstart').val());
 var end= new Date($('#txtend').val());
            if (start < end) {

            }

回答by user5541081

My Comparison with the current date

我与当前日期的比较

    function isPastDate(dateText) {
// date is dd/mm/yyyy
        var inputDate = dateText.split("/");
        var today = new Date();
        inputDate = new Date(inputDate[2], inputDate[1] - 1, inputDate[0], 0, 0, 0, 0);
        today = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
        return inputDate < today;
};

回答by abdul qayyum

I think my answer is late, tell me if it is better or not as only this worked for me in v1.10:

我认为我的回答晚了,告诉我它是否更好,因为只有这在 v1.10 中对我有用:

if($('#DateTo').val().split("/").reverse().join("") <= 
$('#DateFrom').val().split("/").reverse().join("")){//Do something}

回答by Mateen

here's more a simplified way from simple java script

这里有更多来自简单 java 脚本的简化方法

<script>
 //get the values
function asdf(){
var fDate=document.forms[0].fromDate.value;
var tDate=document.forms[0].toDate.value;

//Compare

if(fDate>tDate){
        var dateComponentsfrom = fDate.split("/");

//split and convert into date to be more precise

        var fromdate = new Date(dateComponentsfrom[2], dateComponentsfrom[1] - 1, dateComponentsfrom[0]);
        var dateComponentsto = tDate.split("/");
        var todate = new Date(dateComponentsto[2], dateComponentsto[1] - 1, dateComponentsto[0]);
        if(fromdate>todate){
            alert('from date cannot be greater than to date');
            return false;                        
        }
    }
}
</script>

回答by Anoop Isaac

you can use below code to parse through the dates, using the millisecond approach (adding milliseconds present in a day) will not work properly for the daylightsaving.

您可以使用下面的代码来解析日期,使用毫秒方法(添加一天中存在的毫秒数)将无法正常用于夏令时。

for ( beginDate= new Date(startDate.getTime()); beginDate.getTime()<=endDate.getTime(); beginDate.setDate(beginDate.getDate() + 1)) {
            dateRangeArray.push(new Date(beginDate.getTime()));
        }