Calculating Time from an Alpha Field - Using SPLIT & LENGTH

Solved a problem in an elegant manner and want to show off your code? Know a hard-to-find feature? Post it here for the benefit of others. Questions don't belong here.
Post Reply
DeborahMonks
Posts: 11
Joined: Thu Jan 02, 2014 3:00 pm
Has thanked: 8 times
Been thanked: 1 time

Calculating Time from an Alpha Field - Using SPLIT & LENGTH

Post by DeborahMonks » Wed May 14, 2014 3:27 pm

I've been asked to calculate a time, using the 'File Opened Time' field + 30 minutes. We store the 'File Opened Time' within an Alpha field.

Below is one way of doing this; hopefully it will save you some time if you ever have to do the same!

Code: Select all

#================================== 
v-Text = ":" 
v-Time = {Case Opened Time.Text} 
#================================Split hours 
v-H = SPLIT( v-Time , 1 , 2 ) 
#================================Split minutes 
v-M = SPLIT( v-Time , 4 , 2 ) 
#================================Check if minutes >= 30 
IF v-M >= "30" THEN 
    #==============================If so, minus 30
    v-M = v-M - 30
    #==============================Check minute length
    vLen = LENGTH( V-m )
    #==============================If minute length = '1', prefix with '0'
    IF vLen = 1 THEN
        PUT( v-M , {Minutes.Text} )
        v-M = "0" + {Minutes.Text}
    END
    #==============================Add 1 to hours
    v-H = v-H +1 
    #==============================Change 24 hours to 00 
    IF v-H = "24" THEN 
        v-H = "00" 
    END
    #==============================Check hour length
    vLen = LENGTH( v-H ) 
    #==============================If hour length = '1', prefix with '0' 
    IF vLen = 1 THEN 
        PUT( v-H , {Hours.Text} ) 
        v-H = "0" + {Hours.Text} 
    END 
    #==============================Else < 30  
ELSE 
    v-M = v-M + 30 
END 
#==================== 
v-TimeNew = v-H + v-Text + v-M 
result = v-TimeNew

proclaimadmin
Site Admin
Posts: 40
Joined: Wed Nov 30, 2011 9:37 pm
Been thanked: 1 time

Re: Calculating Time from an Alpha Field - Using SPLIT & LEN

Post by proclaimadmin » Mon May 19, 2014 3:06 pm

Thanks for your post Deborah - I'm sure this will come in useful across many casetypes

- ProclaimForum.co.uk -

Code Killer
Posts: 9
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 11 times
Been thanked: 1 time

Re: Calculating Time from an Alpha Field - Using SPLIT & LENGTH

Post by Code Killer » Tue May 14, 2019 2:01 pm

Hi Debs,

Your code above was really helpful to get me started on restricting the input of time in an alpha field. :geek:

In case it's useful for anyone else see the below:

Code: Select all

#================================== Delivery Complete Time 
v-fail = TRUE 
WHILE v-fail = TRUE DO 
    QUESTION( NEWLINE , "Please enter the time the vehicle was delivered (24-hour format EG. 08:00, 13:00, 19:00)" , "ALPHA" ) 
    v-time = return-value 
    v-tlen = LENGTH( v-time ) 
    #================================== Confirm time is right format 
    IF v-tlen <> 5 THEN 
        MESSAGE( NEWLINE , "Please enter the the time in 24-hour format EG. 08:00, 13:00, 19:00" , NEWLINE , NEWLINE , v-time , " is not a valid format" ) 
        v-fail = TRUE  
    ELSE 
        #================================== Confirm time is a valid number 
        v-first = SPLIT( v-time , 1 , 2 ) 
        v-last = SPLIT( v-time , 4 , 2 ) 
        v-newtime = v-first + v-last 
        IF v-newtime >= 0 AND v-newtime <= 2359 THEN 
            MESSAGE( NEWLINE , "Valid Time" , v-newtime ) 
            #PUT( v-time , {On Hire Process - Delivery Complete Time.Text} ) 
            v-fail = FALSE  
        ELSE 
            MESSAGE( NEWLINE , "Please enter the the time in 24-hour format EG. 08:00, 13:00, 19:00" , NEWLINE , NEWLINE , v-time , " is not a valid time" ) 
            v-fail = TRUE 
        END 
    END 
END 
This could be more effective but it now means I get clean HH:MM data (with some user common sense) which I then strip the first 2 characters and the last 2 characters as part of a date-time formula in Crystal reports.

Thanks :D

John

Post Reply