Increment date using javascript

- I have a date field in Notes named RD (Requested Date).
- I have a expected transit time field called TransitTime which is simply a number of days (ie: 5)

I simply want to place some javascript that will take the number of days found in the TransitTime field and increment the RD date by that amount of days to determine the DeliveryDate.

ie:
RD is 1/31/2005
Transit Time is 5
DeliveryDate should be calculated via javascript as 2/4/2005.
LVL 5
snocrossAsked:
Who is Participating?
 
qwaleteeCommented:
I think this will work:

target = new Date();
target.setTime( (NumberOfDays*86400000) + new Date().getTime() );


target now contains the date NumberOfDays in the future
0
 
Sjef BosmanGroupware ConsultantCommented:
Basically:
    rd= new Date(y,m,d)
    dd= rd + tt*24*60*60*1000
    ddfield= dd.getMonth() + "/" + dd.getDay() + "/" + dd.getYear()

Only works in countries with M/D/Y date format. Otherwise, you must use
    ddfield= dd.toUTCString()

Replace ddfield with the correct sequence for your field, like
    document.forms[0].field.value
0
 
HemanthaKumarCommented:
Doesn't notes field calculation (using @Adjust) do the same job !

~Hemanth
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Sjef BosmanGroupware ConsultantCommented:
Yep, but not immediately on-screen, and I suppose that's the idea
0
 
HemanthaKumarCommented:
you can call recalc function.. sno I have mentioned that trick in one of your question I believe
0
 
qwaleteeCommented:
See this iste: http://www.javascriptmall.com/jsc/jsC4Udate.htm#DateTime17
Which has this code:
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var today = new Date()
var targetDate = new Date("12/31/1999") //use full year
var timeAfterTarget = Math.floor(( today.getTime()
        - targetDate.getTime() ) / 86400000)
var msg = "This is day number " + timeAfterTarget + " for this year."
document.write(msg)
//-->
</SCRIPT>
0
 
snocrossAuthor Commented:
Heman, I need to do this in Javascript.  My validations and transit times are all calculated on the fly using Javascript.

Qwaletee, I'm evaluating your code right now, thanks....
0
 
snocrossAuthor Commented:
I've been toying with your code and am having problems;

With your code I can successfully do the following;

TodaysDate - TargetDate = NumberOfDays
(In this example I can calculate 'NumberOfDays' which is nice)

but I can't seem to do what I want which is;

TodaysDate + NumberOfDays = NewDate
(I want to calculate this new date)

Does this make sense?

0
 
qwaleteeCommented:
getTime returns the numeric value of a datetime.  setTime, when applied to a dateTime object, changes that datetime object to represent the same datetime as the number representes.  That number goes up by one per millisecond.  So, see if this helps you:


var second = 1000;
var minute = second * 60;
var hour = minute * 60;
var day = hour * 24;
var today = new Date();
var todayNumber = today.getTime();
var tomorrowNumber = todayNumber + day;
var monthLaterNumber = todayNumber + (day * 30);
var tomorrow = new Date();
tomorrow.setTime(tomorrowNumber);
var monthLater = new Date()
monthLater.setTime(monthLaterNumber);
alert (today + " tomorrow " + tomorrow + " a month later " + monthLater);
0
 
snocrossAuthor Commented:
I have it;

function DeliveryValidate()
{xyz = document.forms[0].EstTurnaround.value;
CurDate = new Date();
CurDateB = new Date(CurDate.getTime()+(xyz * 86400000));
alert(CurDateB)}
0
 
snocrossAuthor Commented:
Wow, that was pretty close to what I had and posted at the same time!!  The points are yours my friend!
0
 
Sjef BosmanGroupware ConsultantCommented:
It seems I just missed the getTime()-call in my first response. For the rest I don't seem much difference. :(

Sjef :)
0
 
snocrossAuthor Commented:
I'm sorry bosman!  I actually somehow totally missed your snipet of code!!  Thanks for your help, I really appreciate it.
0
 
qwaleteeCommented:
Actually, you could probably reduce it to:
CurDateB = new Date(new Date.getTime() + (xyz*86400000));
0
 
snocrossAuthor Commented:
Beautiful.  Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.