/    Sign up×
Community /Pin to ProfileBookmark

Checking if a date is earlier than today

Hi,

I have the following function which compares the two dates coming from a form. (it fills the day,month,year dropdowns and gets the values from there to compare) I want the script also check if the check-in date (starting) is earlier than today.
I tried to find some scripts on the internet to make it but didn’t work.
I’ll be glad if anyone can help.

Thanks
telmessos

Here is the JS file (Makes the comparison in calDuration() function.)

function startFunction()
{
var monthtext=[’01’,’02’,’03’,’04’,’05’,’06’,’07’,’08’,’09’,’10’,’11’,’12’];
var today=new Date()
dayfield=document.getElementById(“day_start”);
monthfield=document.getElementById(“month_start”);
yearfield=document.getElementById(“year_start”);

var this_month=today.getMonth();
var this_year=today.getFullYear();
var days_in_month=32 – new Date(this_year, this_month, 32).getDate();

for (var i=0; i<days_in_month; i++)
dayfield.options[i]=new Option(i+1, i+1)
dayfield.options[today.getDate()-1]=new Option(today.getDate(), today.getDate(), true, true) //select today’s day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today’s month
var thisyear=today.getFullYear()
for (var y=0; y<2; y++)
{
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today’s year

}
function endFunction()
{
var monthtext=[’01’,’02’,’03’,’04’,’05’,’06’,’07’,’08’,’09’,’10’,’11’,’12’];
var myDate=new Date()
myDate.setDate(myDate.getDate()+1); //Initialized to 30 days in the future.

dayfield=document.getElementById(“day_end”);
monthfield=document.getElementById(“month_end”);
yearfield=document.getElementById(“year_end”);

var this_month=myDate.getMonth();
var this_year=myDate.getFullYear();
var days_in_month=32 – new Date(this_year, this_month, 32).getDate();

for (var i=0; i<days_in_month; i++)
dayfield.options[i]=new Option(i+1, i+1)
dayfield.options[myDate.getDate()-1]=new Option(myDate.getDate(), myDate.getDate(), true, true) //select the day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[myDate.getMonth()]=new Option(monthtext[myDate.getMonth()], monthtext[myDate.getMonth()], true, true) //select today’s month
var thisyear=myDate.getFullYear()
for (var y=0; y<2; y++)
{
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(myDate.getFullYear(), myDate.getFullYear(), true, true) //select today’s year

}
function month1Change()
{
var today=new Date();

dayfield=document.dates.day_start;
monthfield=document.getElementById(“month_start”);
yearfield=document.getElementById(“year_start”);

var this_day=10;
var this_month=document.getElementById(“month_start”).selectedIndex;
var this_year=document.getElementById(“year_start”).selectedIndex;

var starting=new Date(this_year,this_month,this_day);
var this_month=starting.getMonth();
var this_year=starting.getFullYear();
this_year=this_year+3;
var days_in_month=32 – new Date(this_year, this_month, 32).getDate();

var num=days_in_month-1; //Just to select the last date of the selected month.

dayfield.options.length=0;
for (var i=0; i<days_in_month; i++)
dayfield.options[i]=new Option(i+1, i+1)
dayfield.options[num]=new Option(days_in_month, days_in_month, true, true);

}
function month2Change()
{
var today=new Date();

dayfield=document.dates.day_end;
monthfield=document.getElementById(“month_end”);
yearfield=document.getElementById(“year_end”);

var this_day=10;
var this_month=document.getElementById(“month_end”).selectedIndex;
var this_year=document.getElementById(“year_end”).selectedIndex;

var starting=new Date(this_year,this_month,this_day);
var this_month=starting.getMonth();
var this_year=starting.getFullYear();
this_year=this_year+3;
var days_in_month=32 – new Date(this_year, this_month, 32).getDate();
var num=days_in_month-1; //Initiated the second date only to the last date of the month.

dayfield.options.length=0;

for (var i=0; i<days_in_month; i++)
dayfield.options[i]=new Option(i+1, i+1)
dayfield.options[num]=new Option(days_in_month, days_in_month, true, true);

}
function calDuration()
{
var cal_duration=new Date();
var one_day=1000*60*60*24;
start_day=document.getElementById(“day_start”).value;
start_month=document.getElementById(“month_start”).selectedIndex;
start_month=start_month-1;
start_year=document.getElementById(“year_start”).value;

end_day=document.getElementById(“day_end”).value;
end_month=document.getElementById(“month_end”).selectedIndex;
end_month=end_month-1;
end_year=document.getElementById(“year_end”).value;

var ending=new Date(end_year,end_month,end_day);
var starting=new Date(start_year,start_month,start_day);
var diff=Math.floor((ending.getTime()-starting.getTime())/(one_day));

if(diff<=0){
alert(“Departure date can’t be earlier than Arrival date”);
startFunction();
return false;
}
return true;
/*else
startRequest();*/ //This is actually for an xmlHttp call. Ignore it.

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 11.2008 — Modify to your needs ...

[code=php]
<script type="text/javascript">
var StartDate='08/16/2007';
var Start = Date.parse(StartDate);
var now=new Date().getTime();

var timeLeft=Start-now;

if(timeLeft < 0){
alert('The start date has passed. ');
} else {
alert('The start date as yet to be. ');
}
[/code]
Copy linkTweet thisAlerts:
@mrhooJun 11.2008 — [CODE]var checkin= Date.parse('6/10/2008')*1;
if(new Date().getTime()< checkin){
alert('Its later than you think');

}
else alert('Time check.');[/CODE]
×

Success!

Help @telmessos spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...