weixin_33670713 2016-05-26 14:38 采纳率: 0%
浏览 9

日期与javascript比较

I want to compare current date with the one stored in db and I am using the following code for this

<script>
$("#registerButton").click(function() {
var first = $("#scantime").val();
var second = $("#scanbartime").val();
if (parseInt(first.replace(/-/g,""),10) > parseInt(second.replace(/-/g,""),10)){
        alert("Ticket has Expired");
    }
    else {
    var signintime =$("#scantime").val();
    var id = $("#id").val();
var y = new Date();
var y1=y.getFullYear() + "-" +(y.getMonth()+1) + "-" + y.getDate();
y.setHours(y.getHours()+4);
y1 = y1 + "  " + y.getHours()+ ":" + y.getMinutes() + ":" + y.getSeconds();
document.getElementById('scanbartime').value = y1;
var expdate=y1;
       $.ajax({
            url: "updatescan.php",
            method: "POST",
       data: {signintime:signintime,expdate:expdate,id:id} , 
dataType:"text",   
            success: function(data)
                        {                   
                        }
        });
    }
    });
</script>

The format of scantime Is like 2016-5-26 21:31:13 and the format of scanbartime is like 2016-05-26 17:31:11.000000 so why I'm using parseint . My problem is that alert wont be enabled if first is greater than second I have also tried

 if (first > second)

but also the alert wont be enabled Thank you

  • 写回答

2条回答 默认 最新

  • weixin_33690367 2016-05-26 14:43
    关注

    You can create Javascript date object and set the value by parsing first and second and then compare:

    > var first = new Date('2016-5-26 21:31:13')
    > var second = new Date('2016-05-26 17:31:11.000000')
    > first > second
    true
    
    评论

报告相同问题?