WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: GypsyMike on January 23, 2014, 05:24:48 PM

Title: Setting a Variable to time of day
Post by: GypsyMike on January 23, 2014, 05:24:48 PM
First, let me say I am not a programmer, so please don't flame me if this is a dumb question.

I want to set a variable equal to a time of day, like Time=10:00am

Then I want to run a loop to increment the time by one hour for X iterations.

I am having trouble, I think, because of the "am", or maybe the time format.

Any help would be greatly appreciated.
Title: Re: Setting a Variable to time of day
Post by: stanl on January 24, 2014, 07:58:44 AM
This is not optimal, but it works

Code (WINBATCH) Select

;Simple hour display using the scriptcontrpol
;Stan Littlefield
t1 = "2014-01-23 10:00"


oS = CreateObject("MSScriptControl.ScriptControl")
oS.Language = "VBScript"
oS.AllowUI = @FALSE
d=""
u=oS.Eval(: 'FormatDateTime(CDate("%t1%"),3)' )
d=d:u:@CRLF
For i =1 to 10
   t2=oS.Eval(: 'FormatDateTime(DateAdd("h",%i%,"%t1%"),3)' )
   d=d:t2:@CRLF

Next
oS = 0

Message("",d)

Exit
Title: Re: Setting a Variable to time of day
Post by: Deana on January 24, 2014, 08:43:29 AM
I recommend working with the time values then concatenate the AM. WinBatch uses a special YMDHMS format for date times: YYYY:MM:DD:HH:MM:SS. Also WinBatch offers the functions TimeAdd and TimeSubtract to manipulate date time values.

Here is a code sample that should help:

Code (winbatch) Select
currenttime = TimeYMDHMS()
For x = 1  to 24
    currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )
    ;parse out hours and mins
    hh = ItemExtract( 4, currenttime, ":" )
    mm = ItemExtract( 5, currenttime, ":" )
    If hh < 12 Then meridiem = "am"
    Else meridiem = "pm"
    formatteddate = hh : ':' : mm : ' ' : meridiem
    Pause( "Next hour", formatteddate )
Next
Exit


Title: Re: Setting a Variable to time of day
Post by: GypsyMike on January 24, 2014, 10:14:06 AM
Thank you both for your replies. I will try to concatenate the AM -  that is within my skill set.
Not sure I could pull off the VB suggestion, but thank you.
Title: Re: Setting a Variable to time of day
Post by: stanl on January 24, 2014, 10:37:01 AM
Quote from: GypsyMike on January 24, 2014, 10:14:06 AM
Not sure I could pull off the VB suggestion, but thank you.

It is not a VB suggestion, it is Winbatch code. Deana's method is sound but returns 24hr time format, which makes pm redundant.  This would help somewhat with that (again not optimal)
Code (WINBATCH) Select

currenttime = TimeYMDHMS()
For x = 1  to 24
    currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )
    ;parse out hours and mins
    hh = ItemExtract( 4, currenttime, ":" )
    mm = ItemExtract( 5, currenttime, ":" )
    If hh < 12
       meridiem = "am"
    Else
       If hh>12
          hh=hh-12
       Endif
       meridiem = "pm"
    Endif
    formatteddate = hh : ':' : mm : ' ' : meridiem
    Pause( "Next hour", formatteddate )
Next
Exit

Title: Re: Setting a Variable to time of day
Post by: Deana on January 24, 2014, 11:31:00 AM
Good Point Stan. I was lazy and didn't add the code to convert the twenty four hour back to twelve. Thanks for sharing that code addition. You might also want to handle for 0 (midnight).

Code (winbatch) Select
currenttime = TimeYMDHMS()
For x = 1  to 24
    currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )
    ;parse out hours and mins
    hh = ItemExtract( 4, currenttime, ":" )
    mm = ItemExtract( 5, currenttime, ":" )
    If hh < 12
       meridiem = "am"
       if hh == 0 then hh = 12 ; CONVERT MIDNIGHT TO 12
    Else
       meridiem = "pm"
       if hh > 12  ; CONVERT 24 HOUR CLOCK TO 12
          hh=hh-12
       Endif
    Endif
    formatteddate = hh : ':' : mm : ' ' : meridiem
    Pause( "Next hour", formatteddate )
Next
Title: Re: Setting a Variable to time of day
Post by: GypsyMike on January 24, 2014, 12:11:12 PM
Thank you both.

Stanl -  My mistake - when I saw "VBScript", I read it as Visual Basic Script" - thought to myself "Over my head" and stopped reading.

The 24 hour issue you raise is very important for tasks that cross midnight; i.e., start at 10:00 pm and end at 5:00 am.

-----------------------------------------------

Deanna - Thank you very much.

BTW, I manually enter the beginning time in the script, and then I want to add one hour, run another batch file to deal with records that fall within that interval (e.g., time cards), add another hour, run another batch file for records in that group, and so on.

So, if I want to set the beginning time to 10:00 am in your example,  would I enter

Currenttime = TimeYMDHMA("2014:01:24:10:00:00"), with the last numbers zero for AM?

If so, then I guess your next line will bump the variable to 11:00 am?

currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )

If all that is correct, given the last code you provided, if I want to start at 10 pm, what would the last 2 numbers be instead of zero? 12? Is this correct syntax for pm?
Currenttime = TimeYMDHMA("2014:01:24:10:00:12")
Title: Re: Setting a Variable to time of day
Post by: Deana on January 24, 2014, 01:02:37 PM
WinBatch's date time format is a twenty four hour clock ( think military time ). Therefore all hours 12 or greater are considered PM.

Your input should simply be a YMDHMS formatted string ( in the format "YYYY:MM:DD:HH:MM:SS")

The code would look like this for 10AM:

Code (winbatch) Select

currenttime = "2014:01:24:10:00:00" ;10 AM
currenttime = "2014:01:24:22:00:00" ;10 PM (indicated by 22 on a 24 hour clock)
Pause("Start time", currenttime)
For x = 1  to 24
    currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )
    ;parse out hours and mins
    hh = ItemExtract( 4, currenttime, ":" )
    mm = ItemExtract( 5, currenttime, ":" )
    If hh < 12
       meridiem = "am"
       if hh == 0 then hh = 12
    Else
       meridiem = "pm"
       if hh > 12
          hh=hh-12
       Endif
    Endif
    formatteddate = hh : ':' : mm : ' ' : meridiem
    Pause( "Next hour", formatteddate )
Next


Another approach if you only want to pass in the HOUR ( 0 = midnight & 23 = 11PM).

Code (winbatch) Select
;0 = Midnight
;12 = Noon
;23 = 11 PM
starthour = 1 ; 1:00 am
currenttime = StrSub( TimeYMDHMS(), 1, 11 ): StrFixLeft( starthour, 0, 2):":00:00"
Pause('Start time',currenttime)
For x = 1  to 24
    currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )
    ;parse out hours and mins
    hh = ItemExtract( 4, currenttime, ":" )
    mm = ItemExtract( 5, currenttime, ":" )
    If hh < 12
       meridiem = "am"
       if hh == 0 then hh = 12
    Else
       meridiem = "pm"
       if hh > 12
          hh=hh-12
       Endif
    Endif
    formatteddate = hh : ':' : mm : ' ' : meridiem
    Pause( "Next hour", formatteddate )
Next
Title: Re: Setting a Variable to time of day
Post by: GypsyMike on January 24, 2014, 02:39:41 PM
Sweet!

Thank you, Deana.
Title: Re: Setting a Variable to time of day
Post by: GypsyMike on February 05, 2014, 10:03:51 AM
Hi Deana -

I'm snowed in in Scranton, so I have time to work on this.

I ran the code you provided - works great, but I have one newbie question.

How do I paste the NewTime value in an application like Excel?

I typically use Winbatch to open MS Office programs, then use Sendkeys to copy and paste values.

Thank you.

Mike
Title: Re: Setting a Variable to time of day
Post by: GypsyMike on February 05, 2014, 10:46:04 AM
This is terrible code, but you can see what I'm trying to do. Once I can paste the value in Excel I will add a WHILE loop to add an hour for x number of loops.

------------------------- Newbie Code, I know it's bad, OK? -----------------

;TITLE:         1Date_Variable v2
;DATE:         Wednesday, February 5, 2014
;AUTHOR:         Mike Matthews
;VERSION:      2.0 (Deana provided version 1)
;UPDATED:
;PURPOSE         This is just a test - it adds an hour to the start time and pastes the value in Excel

;OPERATE:      Only this file


Display(5,"Deana Code test","Add One Hour to Start Time")

Intcontrol(29,@tab,0,0,0)

Run("C:\Program Files (x86)\Microsoft Office\Office12\Excel","")
Timedelay(3)

;set variables


Clipboard=0
currenttime = "2014:01:24:22:00:00" ;10 PM (indicated by 22 on a 24 hour clock)

For x = 1  to 24
    currenttime = TimeAdd( currenttime, "0000:00:00:01:00:00" )
    ;parse out hours and mins
    hh = ItemExtract( 4, currenttime, ":" )
    mm = ItemExtract( 5, currenttime, ":" )
    If hh < 12
       meridiem = "am"
       if hh == 0 then hh = 12
    Else
       meridiem = "pm"
       if hh > 12
          hh=hh-12
       Endif
    Endif
    formatteddate = hh : ':' : mm : ' ' : meridiem

Clipboard=formatteddate

WinActivate("~Excel")

Sendkey("^g")                ;open GO TO Dialog Box
TimeDelay(1)
Sendkey("A1")             ;GO TO CELL A1 with the url name
TimeDelay(1)
Sendkey("~")                ;carriage return to confirm GO TO cell A1
TimeDelay(3)
Sendkey("^v")                ;CTL+V to paste value of NEXT HOUR
TimeDelay(3)

Exit
Title: Re: Setting a Variable to time of day
Post by: GypsyMike on February 05, 2014, 11:43:25 AM
Never mind.

Used ClipPut(formatted date)

That works.
Title: Re: Setting a Variable to time of day
Post by: td on February 05, 2014, 01:18:45 PM
Like those self answered questions.  8)