Home > JavaScript > Javascript validation : Date Comparision not working

Javascript validation : Date Comparision not working

In most of the web application , Javascript is one of the must neededscripting language. There are some problem happens when you will writelogical code in javascript like if condition, for loop etc.

In some case when you are working around dates you need to compare two dates. What you are doing is take value of date using Element id orElement name using getElementById or getElementsByName. and than compare it using ‘>’ or ‘<‘ operator. At the end you will not get result ofcomparison as expected. Look at the below code which is almost similar to what you had written.

var date1 = document.getElementById(‘date1’).value;
var date2 = document.getElementById(‘date2’).value;
alert(“date1 is greater than date2!”);

Its because you are comparing two strings not two dates. You have to say javascript that you want to compare two dates. What you are missing is convert the value of elements in to Date object. Look at below code to convert string in to date object.Now if you will compare two values than it will compare two dates and gives correct result.

var date1 new Date(document.getElementById(‘date1’).value);
var date2 new Date(document.getElementById(‘date2’).value);
alert(“date1 is greater than date2!”);

To know more about programming,JavaScript issues,jQuery,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.

Categories: JavaScript
  1. top eleven hack download
    March 4, 2014 at 11:42 am

    I think what you posted made a bunch of sense. However, think on this,
    suppose you typed a catchier post title? I ain’t suggesting
    your information isn’t solid., however suppose you added a headline to possibly get folk’s attention?
    I mean Javascript validation : Date Comparision not working | Bhavin
    Rana is kinda vanilla. You should glance at Yahoo’s front page and see how they create post headlines to grab people to click.
    You might try adding a video or a related picture or two to get
    people excited about everything’ve got to say.
    Just my opinion, it might bring your blog a little bit more interesting.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: