The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

Javascript Date Verify Whether A Date Falls Within Any Given Time Range

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 10,652 Tagged by its author as: Programming Javascript Characters (in origin): 9,055 (pages: ~ 3)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: given a javascript date object verify whether such date falls within a specific time range in the past and future both; the point of reference from which this range is assumed must be another date object.  
Similar topic: Javascript Sum Dates: Add Or Subtract Any Amount Of Time From Any Given Date
function setDateRange(dateObjectToTest, referencedateObject, rangePlus, rangeMinus/*pass no less sign*/, time){
if(typeof dateObjectToTest!='object'){dateObjectToTest=new Date();};
if(typeof referencedateObject!='object'){referencedateObject=new Date();};
rangePlus=parseFloat(rangePlus)||0;
rangeMinus=parseFloat(rangeMinus)||0;
var sP=0, mP=0, hP=0, dP=0, MP=0, yP=0;
var sL=0, mL=0, hL=0, dL=0, ML=0, yL=0;
switch(time){
case 0: dP=rangePlus; dL=rangeMinus; break;
case 1: MP=rangePlus; ML=rangeMinus; break;
case 2: yP=rangePlus; yL=rangeMinus; break;
case 3: hP=rangePlus; hL=rangeMinus; break;
case 4: mP=rangePlus; mL=rangeMinus; break;
case 5: sP=rangePlus; sL=rangeMinus; break;
default: dP=rangePlus; dL=rangeMinus;
}
var past=new Date(referencedateObject.getFullYear()-yL, referencedateObject.getMonth()-ML, referencedateObject.getDate()-dL, referencedateObject.getHours()-hL, referencedateObject.getMinutes()-mL, referencedateObject.getSeconds()-sL);
var future=new Date(referencedateObject.getFullYear()+yP, referencedateObject.getMonth()+MP, referencedateObject.getDate()+dP, referencedateObject.getHours()+hP, referencedateObject.getMinutes()+mP, referencedateObject.getSeconds()+sP);
return ( dateObjectToTest.getTime()>=past.getTime() && dateObjectToTest.getTime()<=future.getTime() )?true:false;
/*keep this comment to reuse freely
http://www.fullposter.com/?1*/}
Remove colors  
 
INPUTS: first argument a date object representing the date to check (if passed as boolean false, defaults to the current date).  
Second argument, a reference date object which poses as the date before which and after which the allowed time range has to span (if passed as boolean false, defaults to the current date).  
Third argument a number representing the amount of time of the future edge of the allowed range time.  
Fourth argument an unsigned (that is, with no minus - sign before it, unless as documented later on you want to set a time range all placed within the future) number representing the amount of time of the past edge of the allowed range time.  
Last argument a number (defaults to zero) indicating the time unit (days? hours? months?) that the previous two arguments represent, and it allows the following numerical values:
  • 0=days
  • 1=months
  • 2=years
  • 3=hours
  • 4=minutes
  • 5=seconds
RETURNS: boolean false if the date to test is out of the given range, or boolean true otherwise.  
 
Caveats: javascript months start counting from zero, so January=0, February=1 ... December=11  
 
The constructor of the javascript object Date already takes care of calculating the right year month and day given any time addition or subtraction. It is much safer to rely on this capacity of the constructor, than on manually setting time additions or subtractions via further calls to methods such as (for instance) setDate(), setMonth(), etc.  
Whoever envisions using the methods instead than the constructor of the Date object in order to add or to subtract time units, may incur sooner or later into the notion that the chosen method typecasts exceeding time units by looping recursively through the method's own time unit, never involving any other time unit.  
Thence, subtracting (for instance) days via setDate() rather than via the constructor new Date() may shift the day of the given amount of days indeed, but may never affect months and years; thus, from say January 1 2000 you may subtract 1000 days only to find yourself still in January 2000, because the method keeps subtracting days over and over again, but always within the same month, recursively.  
Example of such wrong code:
show hidden contents: CLICK HERE
 
If you want to place a time range in the future, pass the argument rangeMinus as a negative number: it will denote the time span (say: days) in the future from which the range starts. Then pass rangePlus as a number always positive yet bigger than the passed rangeMinus: that will set the amount of time units (say: days) in the future that represent where the range ends.  
If you plan on setting ranges in the future, it is your responsibility to be sure that these conditions are met: rangeMinus passed as negative, rangePlus as positive and higher than rangeMinus.  
 
Example of correct use:
<script>
//include your setDateRange function here, then:

var referenceDate=new Date(2011, 0/*=January*/, 1, 0, 0, 0);
//
var dateToTestExample=new Date(2010, 11/*=December*/, 25, 0, 0, 0);/*from 25 to reference: 
	dec 26 one day,
	dec 27 two days,
	dec 28 three days,
	dec 29 four days,
	dec 30 five days,
	dec 31 six days,
	jan 1 seven days
	condition of range within 7 days more or less: TRUE
	*/
//
var check=setDateRange(dateToTestExample, referenceDate, 7, 7, 0);
alert(check);

//another test:

var referenceDate=new Date(2011, 4, 5, 7/*hours*/, 30/*minutes*/, 0);
//
var dateToTestExample=new Date(2011, 4, 5, 8/*hours*/, 0/*minutes: make this 1, and will return false*/, 0);
//
var check=setDateRange(dateToTestExample, referenceDate, 30, 30, 4/*MINUTES*/);
alert(check);

/*another test: a range in the future: pass rangeMinus as negative, rangePlus as positive AND higher of the desired amount of days*/

var referenceDate=new Date(2011, 1, 12, 0, 0, 0);
//
var dateToTestExample=new Date(2011, 1, 20/*set this to 19 or 27 and shall return false*/, 0, 0, 0);
//
var check=setDateRange(dateToTestExample, false, 14/*7+7, placing the ending edge*/, -7/*7 to get a week from now, placing the starting edge*/, 0);/*Checked date must be in the future: 7 days from reference date at least, 7 more days from thence at most*/
alert(check);

</script>
Remove colors
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 0.00, Voters: 0)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 10,652
Overall visits to all the topics: 4,984,164
Daily average (Calculated from the website subscription day): 2,065.55
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Javascript
Current order: normal
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(12), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(2), Tarots(1)
Showing topics: 1, 10
Available total: 448
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
Fighting Competently: Anticipation, And Remember It's In His Eyes Identification Number: 465 Visitors: 448 La Musa Segreta: Superiorità Onnipervasiva Della Boxe Identification Number: 464 Visitors: 1,263 Ha Senso Il Doping Sportivo? Effetto Matteo Nello Sport E Business Sportivo Identification Number: 463 Visitors: 1,466 The Musicians Within The Music Box And Other Hereafter Stories Identification Number: 462 Visitors: 2,725 Freud And Jung In A Nutshell: Three Or So Shots At Psychoanalysis For Dummies Identification Number: 461 Visitors: 4,189 Division The Math Of Gods: Ambiguities Of Antanairesis And New Math Operations Identification Number: 460 Visitors: 4,493 The Meaning Of Cruelty Identification Number: 459 Visitors: 4,914 Dÿanèra Ad Eleusi: La Folgorazione Ontologica: Il Pensare Sistematico E Non Identification Number: 458 Visitors: 5,653 Creative Writing: How To Write A Novel. Best Tips From The Bester Professionals Identification Number: 457 Visitors: 7,540 Newsreel Pseudo Intel: The Middle East Approaching The New 20s Identification Number: 456 Visitors: 7,485
External services
This page of this subscriber uses external services: Hide