: Subtracting Dates in InTouch

 

NOTE:  This article and all content are provided on an "as is" basis without any warranties of any kind, whether express or implied, including, but not limited to the implied warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall Q-mation be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortuous action, arising out of or in connection with the use or performance of information contained in this article.

 

TIP: Subtracting Dates in InTouch

Date manipulation in the InTouch environment is pretty straight-forward...  if you know what functions to leverage. Add-in Script Functions combined with Quick Functions can allow you to build reusable scripts to address all of your date manipulation needs.

 

Wonderware has traditionally used the “number of seconds since 1-1-1970”  as the starting point for dates and times in the runtime environment. The ITXtras Script Function Library, available to Comprehensive Support subscribers on the Wonderware website, includes several useful functions including some that are used to convert and create dates in this format.

 

One example if this ability is shown below – creating a simple function to subtract two datetime values. This type of function is useful when you need to set the length of a Historical Trend Chart based on a completed batch run. This example uses the following function from the ITXtras library:

 

ITXCreateDateTimeUTC

Usage : IntegerDateTimeValue = ITXCreateDateTimeUTC(year,month,day,hour,minute,second);

Example : TrendTag.ChartStart = ITXCreateDateTimeUTC(1994,5,10,12,30,0);

 

This function returns an integer containing the number of seconds since January 1, 1970 on base of the system (UTC) time. It is in the correct format for assignment to the .ChartStart dot field of a historical trend.

 

NOTE: This integer date/time format refers to the date/time representation used by historical trend objects, NOT the date/time format used by the $DateTime system variable.


QuickFunction:         getDateDiff ()
Synchronous

 

Function:

getDateDiff(StartDate, EndDate)

Description:

This function will compute difference between two supplied dates and return the difference in seconds. This time is useful when setting the .ChartLength parameter of a historical trend chart.

Script:

{    This function will return time difference in Seconds of two DateTime Strings. The arguments must be entered in 'MM/dd/yyyy hh:mm:ss' format. The required arguments are the following:

 

    t1 <Message>:    "Start" date. NOTE: Date and Time formats must be 'MM/dd/yyyy hh:mm:ss'

    t2 <Message>:    "End" date. NOTE: Date and Time formats must be 'MM/dd/yyyy hh:mm:ss'

 

            offset = call getDateDiff("11/01/2005 08:00:00", "11/01/2005 09:00:00");

 

    NOTE: offset will be negative if t2 is before t1

    NOTE: This QuickFunction requires the add-in Script Function Library - ITEXTRAS

}

 

DIM sMonth AS INTEGER;

DIM sDay AS INTEGER;

DIM sYear AS INTEGER;

DIM sHour AS INTEGER;

DIM sMinute AS INTEGER;

DIM sSecond AS INTEGER;

DIM sSeconds AS INTEGER;

 

DIM eMonth AS INTEGER;

DIM eDay AS INTEGER;

DIM eYear AS INTEGER;

DIM eHour AS INTEGER;

DIM eMinute AS INTEGER;

DIM eSecond AS INTEGER;

DIM eSeconds AS INTEGER;

 

sMonth = StringToIntg( StringMid( t1, 1, 2 ) );

sDay = StringToIntg( StringMid( t1, 4, 2 ) );

sYear = StringToIntg( StringMid( t1, 7, 4 ) );

sHour = StringToIntg( StringMid( t1, 12, 2 ) );

sMinute = StringToIntg( StringMid( t1, 15, 2 ) );

sSecond = StringToIntg( StringMid( t1, 18, 2 ) );

sSeconds = ITXCreateDateTimeUTC(sYear,sMonth,sDay,sHour,sMinute,sSecond);

 

eMonth = StringToIntg( StringMid( t2, 1, 2 ) );

eDay = StringToIntg( StringMid( t2, 4, 2 ) );

eYear = StringToIntg( StringMid( t2, 7, 4 ) );

eHour = StringToIntg( StringMid( t2, 12, 2 ) );

eMinute = StringToIntg( StringMid( t2, 15, 2 ) );

eSecond = StringToIntg( StringMid( t2, 18, 2 ) );

eSeconds = ITXCreateDateTimeUTC(eYear,eMonth,eDay,eHour,eMinute,eSecond);

 

 

RETURN eSeconds - sSeconds;

 

 

Please feel free to email support@qmation.com with questions.

 

©2003 Q-mation, Inc. All rights reserved. All trademarks are the property of their respective owners.