#400884 Author: Shivshanker Cheral Member Level: Gold Member Rank: 20 Date: 09/Jul/2009 Rating:  Points: 2 |
The following JavaScript function accomplishes this: <script language="JavaScript" type="text/javascript"> <!--
function days_between(date1, date2) {
// The number of milliseconds in one day var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds var date1_ms = date1.getTime() var date2_ms = date2.getTime()
// Calculate the difference in milliseconds var difference_ms = Math.abs(date1_ms - date2_ms) // Convert back to days and return return Math.round(difference_ms/ONE_DAY)
}
//--> </script>
This function accepts two Date object arguments—date1 and date2. Note that is doesn't matter which date is earlier or later because this function calculates the absolute value of the difference between them. The constant ONE_DAY stores the number of milliseconds in a day, and then the two dates are converted into milliseconds using the getTime() method. The results are stored in the variables date1_ms and date2_ms. Next the following statement calculates the absolute value, in milliseconds, of the difference between the two dates:
var difference_ms = Math.abs(date1_ms - date2_ms)
This difference is then converted into days by dividing it by the ONE_DAYS constant. (Math.round() ensures an integer result.) Here's the JavaScript code that uses this function to calculate the number of days left until the end of the year:
<script language="JavaScript" type="text/javascript"> <!--
// Store the current date and time var current_date = new Date()
// Store the date of the next New Year's Day var new_years_date = new Date() new_years_date.setYear(new_years_date.getFullYear() + 1) new_years_date.setMonth(0) new_years_date.setDate(1)
// Call the days_between function var days_left = days_between(current_date, new_years_date)
// Write the result to the page if (days_left > 1) { document.write("<b>There are " + days_left + " days left this year.</b>") } else { document.write("<b>There is " + days_left + " day left this year.</b>") }
//--> </script>
Thanks Shivshanker Cheral "If you share your assets (money etc..) it will decrease, But if you share your knowledge it will increase!!! ".
|
#400886 Author: Shivshanker Cheral Member Level: Gold Member Rank: 20 Date: 09/Jul/2009 Rating:  Points: 2 |
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); function countup(yr,m,d) { var today=new Date(); var todayy=today.getYear();
if ((navigator.appName == "Microsoft Internet Explorer") && (todayy < 2000)) todayy="19" + todayy; if (navigator.appName == "Netscape") todayy=1900 + todayy;
var todaym=today.getMonth(); var todayd=today.getDate(); var todaystring=montharray[todaym]+" "+todayd+", "+todayy; var paststring=montharray[m-1]+" "+d+", "+yr; var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1);
document.write("JSS was created " + difference + " days ago.");
} countup(1997,08,29); // Date in format: (year,month,day) // End --> </script>
<p><center> <font face="arial, helvetica" size="-2">Free JavaScripts provided<br> by <a href="http://javascriptsource.com">The JavaScript Source</a></font> </center><p>
Thanks Shivshanker Cheral "If you share your assets (money etc..) it will decrease, But if you share your knowledge it will increase!!! ".
|
#400887 Author: Sajid P K Member Level: Gold Member Rank: 316 Date: 09/Jul/2009 Rating:  Points: 2 |
Hi,
<script type="text/javascript">
//Set the two dates today=new Date() var christmas=new Date(today.getFullYear(), 11, 25) //Month is 0-11 in JavaScript if (today.getMonth()==11 && today.getDate()>25) //if Christmas has passed already christmas.setFullYear(christmas.getFullYear()+1) //calculate next year's Christmas //Set 1 day in milliseconds var one_day=1000*60*60*24
//Calculate difference btw the two dates, and convert to days document.write(Math.ceil((christmas.getTime()-today.getTime())/(one_day))+ " days left until Christmas!")
</script>
More: Link: http://www.javascriptkit.com/javatutors/datedifference.shtml
Do not forget to Rate the post... Regards, Sajid P K Me , Here
|
#400941 Author: Krishnaveni Member Level: Gold Member Rank: 0 Date: 09/Jul/2009 Rating:  Points: 2 |
Date.prototype.DaysBetween = function(){ var intMilDay = 24 * 60 * 60 * 1000; var intMilDif = arguments[0] - this; var intDays = Math.floor(intMilDif/intMilDay); return intDays; } -- pass dateformat as mm/dd/yyyy var dateBits = eval("document.getElementById('"+aFieldIDs[x]+"')"); var aStartDate =new Array(); aStartDate=dateBits.value.split("/"); var fromDates = aStartDate[1] + '/' + aStartDate[0]+ '/' + aStartDate[2] ;
var d1 = new Date(fromDates); var dateBits1 = eval("document.getElementById('"+aFieldIDs[x+1]+"')"); if(dateBits1.value.trim() =="" || dateBits1.value==null) { errorlbl.innerHTML = aenterMessage[x+1]; errorlbl.className="alert"; window.scroll(0,0); return false; } var aEndDate =new Array(); aEndDate=dateBits1.value.split("/"); var ToDates = aEndDate[1] + '/' + aEndDate[0] + '/' + aEndDate[2] ;
var d2 = new Date(ToDates);
if(d1.DaysBetween(d2) <0) { errorlbl.innerHTML =aSplCharMessage[x]; window.scroll(0,0); return false; }
|