Search code examples
xmlstringvbadatetimedate-arithmetic

Read a Date and time string in ParcView and Subtract to find Difference


I am very new to programming and have done most my coding in VBA. That being said still very amateur.

I'll explain the best I know how my scenario, some info may be self-explanatory but not to me. I'm working with a Trending software (PARCView that reads from a PLC and HMI system to display data. The current PLC tags do not give all the data that I need so this PARCview software allows me to create new tags in order to manipulate data and display trends, efficiencies, etc. that I may want.

It allows me to import and export XML script to do the Calculated tag, but using a VBA format would work best for me so I can understand better. I am able to figure out how to do simple calculations like multiples, dividing of data from a single tag, but now I am struggling to do more complex manipulations.

I would like to find the time in hrs. between two dates and times given. The dates and times are pulled as a string from another software called MiniMint. They are received in the format m/d/yyyy hh:mm:ss. The date does not use 0's as place holders so it can be seen as mm/d/yyyy or mm/dd/yyyy as well. I do not understand how to take this string of "text" and read it correctly as numbers to do the calculations I would like.

The two tags I would like to subtract are called FL4 End Run Time and FL4 Scheduled Start and both are received in the format listed above.

So I don't know where to start and appreciate the help greatly!


Solution

  • I'm not sure about the tags, PARCView, PLC and HMI, but in VBA, I would use the following to convert a string date like that into a date object:

    Function String2Date(inString As String) As Date
    
        Dim sp() as String
        Dim month as Integer, day as Integer, year as Integer
    
        sp() = Split(inString, "/")    'splits the string into an array, using /
        month = CInt(sp(0))  'CInt forces a conversion to integer from string, just in case
        day = CInt(sp(1))
        year = CInt(sp(2))
    
        String2Date = DateSerial(year, month, day)    'date serial returns a date from integer year, month, day arguments
    End Function