cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

how to calculate downtime status using from date and end date.

Suraj2692
12-Amethyst

how to calculate downtime status using from date and end date.

Hi, Everyone

I want to calculate the downtime status which shows in below image,

I am creating a program that has 'from_date and 'to_date' and their program so I want to display time status which is shown in the below image.

use case.

  1. Want to show the time if the program will start in 5hours (ex.' start in 5 hr',4,3,2,1)
  2. then change the time when it's less than an hour(ex. 'start in 59 min',58,57, etc till 0)
  3. change the time status when it's less than a minute (ex. 'start in 59 sec',58,57, etc till 0)
  4. If the program will start, the status will 'ON'(ex.' ON-59 min remain')
  5. If the program time will end (ex, 'END'

Untitled.png

can you please help how can I do that,

4 REPLIES 4
PaiChung
22-Sapphire I
(To:Suraj2692)

I would say the best way to approach this is to generate the counter Server side - this does mean you need to know what time zone the user is in potentially.

A timer or scheduler runs and generates the appropriate number and using GetProperties push on update your information is pushed to the connected mashup.

Turning that around, you can use a Refresh timer on the mashup to call a service to get the latest number to display.

As a third option you might be able to do it with an expression as well 

Suraj2692
12-Amethyst
(To:PaiChung)

So I have to create a service or is there any option to create downtime
status from timer or scheduler.
If timer or schedular i used then how can I get the program start time and
end time.
Can you tell me any example?
PaiChung
22-Sapphire I
(To:Suraj2692)

There isn't an OOTB countdown available.

So you have to calculate the time remaining from some initiating event.

You need to determine what you will use to start the countdown.

This could be the same scheduler/timer to check a calendar or a button on a mashup, or a change in information in the system.

Suraj2692
12-Amethyst
(To:PaiChung)

I have created a thingworx service that calculates the downtime status but I am not getting actual time difference between date-time. please check the below code please correct me where I was wrong 

var row ;
var milliseconds;
var d1 ;
var d2 ;
var date = new Date();
//var time = new Date();
var diff;
var timeINString;
// result: INFOTABLE dataShape: ""
var  result = Things["PostgreSQL_DB"].getAllProgramList();
result.AddField({name : "difference", baseType : "STRING"});
var tableLength =result.rows.length;
for (var x=0; x < tableLength; x++) {
    row = result.rows[x];
    d1 = row.from_time;
    d2 = date;
    d3 = row.to_time;
    // date1:DATETIME
    var date1 = new Date();
    // dateDifference(date1:DATETIME, date2:DATETIME):NUMBER
    var timediiff = dateDifference(d1, date1);
//    row.difference =timediiff;
    var t = new Date(timediiff);
     if (t.getHours() < 5 && t.getHours() > 1) {
        	row.difference ='start in'+ t.getHours() +' hours'; 
        }else if(t.getMinutes() < 60 && t.getMinutes() > 1 ){
            row.difference ='start in'+ t.getMinutes()+' minutes';
        }else if(t.getSeconds() < 60 && t.getSeconds() < 1){
            row.difference ='start in'+ t.getSeconds()+' second';
        }else if(t <= row.from_time){
            row.difference ='ON';
        }else if(t > row.to_time){
            row.difference ='OFF';
        }
     }

here is my output please check the program from_time and to_time and system current time.
The problem I am facing is a difference of two times(according to program) is not correctly shown in the difference column.

Example.

program id 54 will start on 2020-10-06 19:00:00.000 and the current time is 2020-10-06 18:37:10.382,
It means the program will start in 27minutes but the result in the different column showing 45 minutes.

I hope you get my point.please suggest me right way
output.png

Top Tags