Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
4

How to calculate multiple future dates from one date

Community Beginner ,
Feb 14, 2024 Feb 14, 2024

I am trying to enter a single pay period start date that leads to the start and end dates for the next 27 pay periods.

 

for the starting pay period I used the following script in each text field cusom calculation script adjusting the field to pull from

var d = util.scand("mm/dd/yyyy", this.getField("PPWK2End1").valueAsString);

var theTime = d.getTime () ;

var oneDay = theTime + 1*24*3600*1000;

var dd = new Date(oneDay);

event.value = util.printd("mm/dd/yyyy", dd);

 

then I used the following to get the payperiod end date 

var d = util.scand("mm/dd/yyyy", this.getField("PPWK1Start2").valueAsString);

var theTime = d.getTime () ;

var oneDay = theTime + 13*24*3600*1000;

var dd = new Date(oneDay);

event.value = util.printd("mm/dd/yyyy", dd);

 

The problem is that the dates do not populate beyond the second pay period and the calculations seem to forget there are 31 days in october when it does run correctly.

 

Please help! there has to be an easier way!

 

Maiah32906686j7xo_0-1707925387576.png

 

TOPICS
How to , JavaScript
828
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 14, 2024 Feb 14, 2024

Use this:

 

var s = this.getField("PPWK1Start2").valueAsString;
if (s=="") event.value = "";
else {
	var d = util.scand("mm/dd/yyyy", s);
	d.setDate(d.getDate()+13);
	event.value = util.printd("mm/dd/yyyy", d);
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2024 Feb 14, 2024

You want to populate 27 days from start date?

Why not use getDate() and setDate() instead of variable oneDay?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 14, 2024 Feb 14, 2024

the PPWK2End is 13 days past PPWK1Start and the next row is the start of a new 2 week pay period 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2024 Feb 14, 2024

Use this:

 

var s = this.getField("PPWK1Start2").valueAsString;
if (s=="") event.value = "";
else {
	var d = util.scand("mm/dd/yyyy", s);
	d.setDate(d.getDate()+13);
	event.value = util.printd("mm/dd/yyyy", d);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 14, 2024 Feb 14, 2024
LATEST

this totally worked! 

 

I ended up going off of the first dte for each other date so there was some math involved but it works perfectly!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines