2

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. :(

Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
Amit Pandey
  • 79
  • 1
  • 2
  • 11
  • You're comparing strings when you should be comparing date objects. Basically you're doing it backwards, as you should convert the `usrDate` string to a date object, and not the date object in `current****` to a string. – adeneo Apr 01 '13 at 11:58
  • why not use plain javascript? use the `gettime()` function to convert the dates into ticks then you have two numbers you can compare. – Sven Bieder Apr 01 '13 at 11:59
  • use `if(Date.parse(usrDate) > Date.parse(currentcpDateTime))` – Manish Mishra Apr 01 '13 at 11:59
  • You actually *can* compare dates as strings, but not if you use `month/day/year` format. It's the same as when you add dates to file names. – Álvaro González Apr 01 '13 at 12:02

2 Answers2

3

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

Your HTML

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

Your 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');
        }
    });
});

Check out the example at Live fiddle

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
  • Actually I add some hours in current datetime before checking these condition. – Amit Pandey Apr 01 '13 at 12:08
  • You can [add hours](http://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) to your date object. So I don't think its an issue. – Jay Mayu Apr 01 '13 at 12:10
0

check the solution provided here it may help, i use it in my projects.you save time to code everything from scratch. http://trentrichardson.com/examples/timepicker/ .(in the end of the page)

timmz
  • 2,194
  • 3
  • 23
  • 29