View Full Version : how to add 3 days to a date and get the result date ?
bugbugb
09-27-2002, 06:29 PM
Hi,
I am trying to add days to a date variable but there does not seem to be any method available in the date object to do this (like dateadd function in vbscript). Can anyone please help me out ?
Bugbugb
mpjbrennan
09-27-2002, 08:07 PM
Javascript date values are calculated in milliseconds. Try the following:
var oneMinute = 60*1000
var oneHour = oneMinute*60
var oneDay = oneHour*24
var threeDays = oneDay*3
var targetDate = new Date()
dateInMs = targetDate.getTime()
dateInMs += threeDays
targetDate.setTime(dateInMs)
this could be made more concise but I thought it would be clearer in this format
(based on Danny Goodman's "Javascript Bible")
patrick
aCcodeMonkey
09-27-2002, 08:16 PM
mpjbrennan,
You have to just need to use the Date() and obj.getDate() functions.
The example below write the current date and the date three days from now.
<script language="javascript">
var nDays = 3;
var oDate = new Date();
var nCurrDay = oDate.getDate();
// Add 'X' days to the current date
var newDay = (nCurrDay+nDays);
//Display the formatted dates
document.write('Current Date: '+oDate.getMonth()+'/'+nCurrDay+'/'+oDate.getYear()+'<br>');
document.write('New Date: '+oDate.getMonth()+'/'+newDay+'/'+oDate.getYear());
</script>
Hope this helps.. :cool:
mpjbrennan
09-27-2002, 10:54 PM
That gives a Current Date of 8/27/2002 and a New Date of 8/30/2002 when I try it. In Mozilla/NS the year is 102
document.write(targetDate.getDate() + "/" + (targetDate.getMonth()+1) + "/" + targetDate.getFullYear()) will print out 30/9/2002 in both IE and Mozilla/NS
patrick
I'll move this thread to the more appropriate Javascript Programming forum, considering this has nothing to do with DOM Scripting.
boywonder
09-28-2002, 12:15 AM
Hi, I am trying to add days to a date variable but there does not seem to be any method available in the date object to do thisto add 3 days to a date, all you need is:
var dt = new Date();
dt.setDate(dt.getDate()+3);
hope that helps
bugbugb
09-28-2002, 08:23 AM
Hello everyone ,
Thanks a lot for your contributions and I did find them very useful as each one has taught me a diferent approach. I did try one out myself and here is what I have come up with. Please let me know if there are any problems with this one
function AddToDate(DateVal,Yrs,Mnths,Days)
{
// DateVal - a start date (date)
// Yrs - years to add (int)
// Mnths - months to add (int)
// Days - days to add (int)
// this function returns the new date resultant of a date addition
var Dt = (DateVal == ""?(new Date()):(new Date(DateVal)))
Yrs = (Yrs == ""?0:parseInt(Yrs))
Mnths = (Mnths == ""?0:parseInt(Mnths))
Days = (Days == ""?0:parseInt(Days))
return new Date(Dt.getFullYear()+Yrs,Dt.getMonth()+Mnths,Dt.getDate()+Days)
}
regards and thanks to all,
bugbugb
COBOLdinosaur
09-28-2002, 07:52 PM
newdate= new Date(olddate.valueof() + (nofofDays*24*3600*1000));
Then format using the date methods of newdate
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.