Skip to main content
Jul 21, 2024 10:12 AM UTC
Solved

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

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);
}
VelkumarAnswer
Jul 22, 2024 5:36 AM UTC

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

 

3 replies

Jul 21, 2024 7:14 PM UTC

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.

 

Aug 8, 2024 7:59 AM UTC

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.

VelkumarAnswer
Jul 22, 2024 5:36 AM UTC

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