How to compare two datetime in javascript?

后端 未结 4 1489
情书的邮戳
情书的邮戳 2021-01-22 23:48

I tried create markers by JSON parse from C #.I have a small problem about datetime compare in javascript.

  var nowDate= new Date();

  var LastTenMin= new Date         


        
4条回答
  •  一整个雨季
    2021-01-23 00:10

    To compare dates with time in Javascript we need to pass them in Date object as "yyyy/mm/dd HH:mm" format for e.g. like "2014/05/19 23:20" . Then you can just compare them with > or < less then symbol according to your business rule. Please see given below code for more understanding.

    $(function () {
    
        $("input#Submit").click(function () {
            var startDateTime = $("input[name='StartDateTime']").val();
            var endDateTime = $("input[name='EndDateTime']").val();
    
            var splitStartDate = startDateTime.split(' ');
            var splitEndDate = endDateTime.split(' ');
    
            var startDateArray = splitStartDate[0].split('/');
            var endDateArray = splitEndDate[0].split('/');
    
            var startDateTime = new Date(startDateArray[2] + '/ ' + startDateArray[1] + '/' + startDateArray[0] + ' ' + splitStartDate[1]);
            var endDateTime = new Date(endDateArray[2] + '/ ' + endDateArray[1] + '/' + endDateArray[0] + ' ' + splitEndDate[1]);
    
            if (startDateTime > endDateTime) {
                $("#Error").text('Start date should be less than end date.');
            }
            else {
                $("#Error").text('Success');
            }
        });
    
    });
    

    You can also see a working demo here

提交回复
热议问题