?
Solved

validating dates javascript (date available/date may be available/date unavailable)

Posted on 2011-09-05
10
Medium Priority
?
276 Views
Last Modified: 2012-06-27
Hello,

I'm trying to make a few different alerts based on date input. The jquery calendar puts the date in this format:

10/27/2011

Is there a way to pop an alert "may or may not be possible " if the date is between 3 and 5 days in the future, alert "date is unavailable" if less than three days in the future. and alert "date is in the past" if it is in the past"?

What I have started is basically just to collect my date from the input field, and set up a few variables.. ><

$('.continue').click(function(){
	//alert('test2');
	var selected_date = $('#ship_date').val();
	//alert (selected_date);
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	var hours = currentTime.getHours();
	//document.write(month + "/" + day + "/" + year)
	

});
	
});

Open in new window


I don't know how to deal with days that roll around into different months. And also, how to treat the 3 day period as the next day if the current time is, say, after 14:00. I appreciate it if you can help me figure out this complex problem!

Thanks!
0
Comment
Question by:hibbsusan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36486380
did you tried extracting day, month and year value from the selected date and comparing?
0
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 668 total points
ID: 36486394
You can get valid future dates by using the date function:


var start = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate() + 3);  // 3 days in the future
var end = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate());  // 5 days in the future

Open in new window

0
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 664 total points
ID: 36486489
To compare 2 dates always follow the format yyyymmdd. They are considered as numeric, it will give perfect result.

For example 20111001 is greater than 20110930.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:Kusala Wijayasena
Kusala Wijayasena earned 668 total points
ID: 36486579
Check this out:

var selected_date = new Date($('#ship_date').val());
var now = new Date();
var now_date = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
var one_day = 1000 * 60 * 60 * 24;
var day_difference = (selected_date.getTime() - now_date.getTime()) / one_day;

//day_difference: for future dates you will get positive numbers and past date you will get negative number

Open in new window


-Kusala
0
 

Author Comment

by:hibbsusan
ID: 36488609
thanks! that's working pretty well!

why does

var one_day

multiply by 1000?
0
 

Author Comment

by:hibbsusan
ID: 36488679
ok. milliseconds. got it. is there any built in functions built in for dealing with weekends?
thanks!
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36489890
this will tell you which day of the week it is
http://www.w3schools.com/jsref/jsref_getday.asp

0
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 36491344
What do you mean by "dealing with weekends" ?

-Kusala
0
 

Author Comment

by:hibbsusan
ID: 36499023
Well, I came up with this code.

Do you think it conforms to good practice? I used pieces provided by you guys, but sort of modified it a bit. I am not experienced dealing with dates like this, so I am lacking a little in confidence...it it quite long also, so I wonder if I am missing something that could help me make it shorter

$(document).ready(function(){

$('.continue').click(function(){
	
	//alert variables
	var can_print = "good";
	var cannot_print = "Unfortunately, we are unable to deliver your project by this required-on date.";
	var maybe_print = "Depending on our current schedule, we might not be able to fulfill that required-on date. However, you may continue requesting this date, and your project will be marked with an urgent status. You will be contacted with details.";
	var weekend = "The delivery date you have requested is a Saturday. This service is available at additional charge.";
	var past = "please select a future date at least three business days from today. If after 3.00pm CST, the order will post tomorrow, and you should choose a required-by date at least three days from that date.";
	
	var selected_date = new Date($('#ship_date').val());
	var now = new Date();
	var now_date = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
	var hour = now.getHours();
	var now_day_of_week = now.getDay();
	var now_day_of_month = now.getDate();
	var is_next_day = new Boolean(); 
	
	var day_difference = Number();
	var required_time_to_print = 3;
	required_time_to_print_weekend = 5;
	//set up this boolean to be true if it is the end of the day and the
	//order must be treated as being submitted the next day
if (!isNaN(selected_date)){	



	if (hour >14)
	{
		
		 is_next_day = true;
		 
		 //make the now_day_of_month value increase by one
		 //because it is the next business day
		 now_day_of_month++;
		 }
		 else
		 {
		 is_next_day = false;
	 }
	
	
	var is_end_of_week = new Boolean(); 
	//set up this boolean to be true if the day of the week is
	//either thursday (4) or friday (5)
	if (now_day_of_week == 4 || now_day_of_week == 5)
	{
		 is_end_of_week = true;
		 //time required to print increases by 2
		 required_time_to_print = required_time_to_print_weekend;
		 }
		 else
		 {
		 is_end_of_week = false;
	 }
	
    var day_difference = (selected_date.getDate() - now_day_of_month);	
	
	if (selected_date.getDay() == 6 || selected_date.getDay() == 7)
	{
		alert(weekend);
		return false;
	}
	
	if (day_difference <= 0 )
	{
		alert(past);
		return false;
	}

	if (day_difference>required_time_to_print)
	{
		//alert('okay to print');
		return true;
	}
	else if (day_difference==required_time_to_print)
	{
		var confirm_box = confirm(maybe_print);
			if (confirm_box==true)
			{
				return true;
				}
				else
				{
				return false;
			}
	}
	else
	{
	alert(cannot_print);	
	return false;		
	}
}			
	});
});

Open in new window

0
 

Author Closing Comment

by:hibbsusan
ID: 36558991
Thanks! got it workin :)

I'm sorry for the delay in the point-distribution..
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question