Solved

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

Posted on 2011-09-05
10
249 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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
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 167 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 166 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
 
LVL 11

Assisted Solution

by:Kusala Wijayasena
Kusala Wijayasena earned 167 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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:gurvinder372
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Read about why website design really matters in today's demanding market.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now