[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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

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
hibbsusan
Asked:
hibbsusan
  • 4
  • 2
  • 2
  • +2
3 Solutions
 
Gurvinder Pal SinghCommented:
did you tried extracting day, month and year value from the selected date and comparing?
0
 
OmniUnlimitedCommented:
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
 
Easwaran ParamasivamCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Kusala WijayasenaSoftware EngineerCommented:
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
 
hibbsusanAuthor Commented:
thanks! that's working pretty well!

why does

var one_day

multiply by 1000?
0
 
hibbsusanAuthor Commented:
ok. milliseconds. got it. is there any built in functions built in for dealing with weekends?
thanks!
0
 
Gurvinder Pal SinghCommented:
this will tell you which day of the week it is
http://www.w3schools.com/jsref/jsref_getday.asp

0
 
Kusala WijayasenaSoftware EngineerCommented:
What do you mean by "dealing with weekends" ?

-Kusala
0
 
hibbsusanAuthor Commented:
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
 
hibbsusanAuthor Commented:
Thanks! got it workin :)

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now