比较日期和时间jQuery中的时间 [英] Compare Date & Time in JQuery

查看:104
本文介绍了比较日期和时间jQuery中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按日期在jquery中比较一些消息. 例如: 我有一个文本框值,由用户端填充为04/01/2013 13:15:32. 并将jquery变量中的当前日期时间作为04/01/2013 18:30:12 如果文本框的值小于当前日期时间,则显示一条消息:

I want to some message by date comparing in jquery. for example: I have a text box value as 04/01/2013 13:15:32 filled by userend. And a current date time in jquery variable as 04/01/2013 18:30:12 if text box value is less then current datetime then show a message:

function foo(){
    usrDate = jQuery("#txtDate").val(); //getting user input date time

    currentDate = new Date(); //system current date
    currentMonth = currentDate.getMonth() + 01;
    currentDay = currentDate.getDate();
    currentYear = currentDate.getFullYear();
    currentHours = currentDate.getHours();
    currentMinutes = currentDate.getMinutes();
    currentSeconds = currentDate.getSeconds();

    currentcpDateTime = currentMonth + "/" + currentDay + "/" + currentYear + " " + currentHours + ":" + currentMinutes + ":" + currentSeconds;
       if (usrDate > currentcpDateTime ) {
       alert("you can not choose date greater then current");   
    }
   else {
      alert("valid date");
 }
}

每次错误的结果. :(

each time wrong result. :(

推荐答案

为什么不使用类似jQuery UI的框架,您可以按照以下步骤简单地完成此操作

Why not use a framework like jQuery UI and you can do this simply as below

您的HTML

<p>Date: <input type="text" id="datepicker" /></p>
<input type="button" id="checkDate" value="CheckDate">

您的JS

$(document).ready(function(){
    $( "#datepicker" ).datepicker();
    $('#checkDate').bind('click',function(){
        var myDAte = $('#datepicker').datepicker( "getDate" );
        var curDate = new Date();

        if(curDate>myDAte){
            alert('current date is high');
        }
        else{
            alert('selected date is high');
        }
    });
});

Live Fiddle 中查看示例kbd>

Check out the example at Live fiddle

这篇关于比较日期和时间jQuery中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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