Skip to main content
13-Aquamarine
July 21, 2024
Solved

Increasing every hours in each iteration using for loop in thingworx.

  • July 21, 2024
  • 2 replies
  • 1495 views
var date = new Date();

for (let index = 0; index < 12; index++) {
    let startDate = new Date(date);
    startDate.setHours(18 + index, 0, 0);
   
    let endDate = new Date(date);
    endDate.setHours(18 + index + 1, 0, 0);
   
    console.log("StartDate " + startDate);
    console.log("EndDate " + endDate);
}
Best answer by Velkumar

Hi @Hrishabh.Sharma 

 

You can use below script to increase startime & endtime by 1 hour  

let startTime = new Date();
// End Time plus 1 hour from startTime
let endTime = dateAddHours(startTime, 1);

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(TestDS1)
let result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
	infoTableName: "InfoTable",
	dataShapeName: "TestDS1"
});

// Iterate
let i = 0;
while (i < 5) {
	/* 
	LOGICS
	*/
	result.AddRow({
		"StartDate": startTime,
		"EndDate": endTime
	});
	// StartTime & EndTime will increase at end of iteration
	startTime = endTime;
	endTime = dateAddHours(startTime, 1);
	i++;
}

 

Output :

Velkumar_0-1721626503641.png

/VR

 

2 replies

19-Tanzanite
July 21, 2024

Hi @Hrishabh.Sharma ,

If you observe any the vast majority of the requests for help in this forum, you will observe that they follow a specific structure, where the user explains what they want to do and what the issue is.

Would you be able to explain what are you trying to achieve and where exactly you encountered issues?

I observed you posted just the code in the section where you should explain these things I mentioned above, and also the title does not clarify these as well.

 

13-Aquamarine
August 8, 2024

Hi @VladimirRosu_116627,

Most of things, i have posted just for future reference to itself and others, I am not asking to anybody for answers.

Velkumar19-TanzaniteAnswer
19-Tanzanite
July 22, 2024

Hi @Hrishabh.Sharma 

 

You can use below script to increase startime & endtime by 1 hour  

let startTime = new Date();
// End Time plus 1 hour from startTime
let endTime = dateAddHours(startTime, 1);

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(TestDS1)
let result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
	infoTableName: "InfoTable",
	dataShapeName: "TestDS1"
});

// Iterate
let i = 0;
while (i < 5) {
	/* 
	LOGICS
	*/
	result.AddRow({
		"StartDate": startTime,
		"EndDate": endTime
	});
	// StartTime & EndTime will increase at end of iteration
	startTime = endTime;
	endTime = dateAddHours(startTime, 1);
	i++;
}

 

Output :

Velkumar_0-1721626503641.png

/VR