-3

Possible Duplicate:
How can I convert string to datetime with format specification in JavaScript?

I have the code given below for validating the start time and end time.

It works to some extend but when I give start time 9:00 AM and End time 1:00 PM it does not work.

$("#dateTimeAddButton").click(function () 
{                       
        var Date = $("#myDatePickerId").val();

        var startTime = $("#start-Time").val();
        var endTime = $("#end-Time").val(); 

        if(startTime > endTime)
           alert('end time always greater then start time');

});

Someone help me with this.Thanks in advance

Community
  • 1
  • 1
Midhun
  • 27
  • 1
  • 3
  • 10
  • No.I am looking for a solution which doesn't use jquery validator plugin. – Midhun Dec 26 '12 at 14:14
  • 1
    So... you're directly comparing the strings "9:00 AM" and "1:00 PM" and wondering why JavaScript doesn't know what those mean? – Blazemonger Dec 26 '12 at 14:15
  • first one is my datepicker id,second one id of the text box which has the start time value,and second one which have the end time value – Midhun Dec 26 '12 at 14:16
  • i just gave a scenario where it does not work.if the end time is less than start time regardless of it is AM or PM,it this does not work. – Midhun Dec 26 '12 at 14:17
  • You need to [convert those strings into datetime objects](http://stackoverflow.com/q/476105/901048) before you can compare them. – Blazemonger Dec 26 '12 at 14:17
  • Ok..let me see that..thankx for the hint – Midhun Dec 26 '12 at 14:21
  • I do not need the date here.I just need the time.Give me a way to get time only please. – Midhun Dec 27 '12 at 05:46
  • Date/time is one object in JavaScript - you need to add SOME date even if you don't use it. – Blazemonger Dec 27 '12 at 13:05

2 Answers2

1

Modification to the solution proposed by @Palash

 var startDate = new Date($('#start-Time').val());
 var endDate = new Date($('#end-Time').val());

 if (startDate.getTime() > endDate.getTime()){
 // Do something 
 } 
Scorpio
  • 1,151
  • 1
  • 19
  • 37
0

You are comparing two strings, you are not comparing Date objects. You need to convert them to a time format.

epascarello
  • 204,599
  • 20
  • 195
  • 236