Click here to Skip to main content
16,022,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 Dates in javascript

1)
var vDate = document.getElementById('AppliedFor' + RowNo).value;

it gives date as
"25/May/2018"


2)
var HDNSessionFromDate1 = document.getElementById('HDNSessionFromDate').value;

it Gives me Date as
"01/Apr/2018"


3)
var HDNSessionToDate = document.getElementById('HDNSessionToDate').value;

it gives Date as
"31/Mar/2019"


now I want To check my date
vDate should be in between <pre>HDNSessionFromDate1 
and
HDNSessionToDate 


What I have tried:

I have 3 Dates in javascript

1)  <pre>var vDate = document.getElementById('AppliedFor' + RowNo).value;

it gives date as
"25/May/2018"


2)
var HDNSessionFromDate1 = document.getElementById('HDNSessionFromDate').value;

it Gives me Date as
"01/Apr/2018"


3)
var HDNSessionToDate = document.getElementById('HDNSessionToDate').value;

it gives Date as
"31/Mar/2019"


now I want To check my date
vDate should be in between <pre>HDNSessionFromDate1 
and
HDNSessionToDate 


now I am converting it into mm/dd/yyyy format for that I have function
GetNewFormat()

var temp = GetNewFormat(vDate);

here I am getting Date  as 05/25/2018

        temp = temp.split("/");
        var month = parseInt(temp[0]);
        var day = parseInt(temp[1]);
        var year =parseInt(temp[2]);


        var temp1 = GetNewFormat(HDNSessionFromDate1);

here I am getting Date  as "04/01/2018"

        temp1 = temp1.split("/");
        var month1 = parseInt(temp1[0]);
        var day1 = parseInt(temp1[1]);
        var year1 = parseInt(temp1[2]);

        var temp2 = GetNewFormat(HDNSessionToDate);

     here I am getting Date  as  "03/31/2019"       

        temp2 = temp2.split("/");
        var month2 =parseInt(temp2[0]);
        var day2 =parseInt(temp2[1]);
        var year2 =parseInt(temp2[2]);
        alert(year2);
        var vDate1 = new Date(year, month, day);
        alert(vDate1);


now I have
var vDate1 = new Date(year, month, day);
        var vDate2=new Date(year1, month1, day1);
        var vDate3=new Date(year2, month2, day2);
        alert(vDate1);


        if (vDate1 >= vDate2 && vDate1 <= vDate3)
        {
            lblresult.innerHTML = "Please Enter Valid Date"
                lblresult.className = 'Red';
                document.getElementById('AppliedFor' + RowNo).focus();
                return false;
        }



but It Gives error at
var vDate1 = new Date(year, month, day);



I created Function which gives date in following format


<pre>function GetNewFormat(datevar) {
    var mon;
    var m1 = datevar.substring(3, 6);

    switch (m1.toLowerCase()) {
        case "jan":
            mon = 01;
            break;
        case "feb":
            mon = 02;
            break;
        case "mar":
            mon = 03;
            break;
        case "apr":
            mon = 04;
            break;
        case "may":
            mon = 05;
            break;
        case "jun":
            mon = 06;
            break;
        case "jul":
            mon = 07;
            break;
        case "aug":
            mon = 08;
            break;
        case "sep":
            mon = 09;
            break;
        case "oct":
            mon = 10;
            break;
        case "nov":
            mon = 11;
            break;
        case "dec":
            mon = 12;
            break;
    }
    datevar = mon + "/" + datevar.substring(0, 3) + datevar.substring(7);
    return datevar;
}
Posted
Updated 15-May-18 3:19am

1 solution

The month used to create a Date() object should be 0-11 instead of 1-12.

JavaScript Date Reference[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900