WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: spl on May 26, 2025, 12:52:07 PM

Title: date conversion ?
Post by: spl on May 26, 2025, 12:52:07 PM
Got a request from a noobie about date conversions. Seems he gets dates in a text file as "20240524_132134" etc... which would need to be converted to a date object, then add a value (i.e. assume a scenario as last charge date => minimum payment due). I quickly suggested a simple conversion but was questioned on returning the serial date back to a date format string. Now I know I could easily do this with PS, or invoking an Excel formula via WB, but was thinking there was a wb function to convert serial date => mm/dd/yyyy hh:mm:ss - I went through the wb functions and felt at one time the conversion had been mastered but can't find or remember.
date="20240524_132134"
date1= strreplace(date,"_","")
date2 = strsub(date1,5,2):"/":strsub(date1,7,2):"/":strsub(date1,1,4):" ":strsub(date1,9,2):":":strsub(date1,11,2):":":strsub(date1,13,2)
date3 = objecttype("DATE",date2)+10
message(date,date2:@LF:date3)
Title: Re: date conversion ?
Post by: JTaylor on May 27, 2025, 04:38:08 AM
TimeFormat()

Jim
Title: Re: date conversion ?
Post by: spl on May 27, 2025, 06:56:43 AM
Quote from: JTaylor on May 27, 2025, 04:38:08 AMTimeFormat()

Jim

No, doesn't work from date Object directly. Had to go into some of my old archives with CLR. Used an .net function for serial dates (Excel),
date="20240524_132134"
date1= strreplace(date,"_","")
date2 = strsub(date1,5,2):"/":strsub(date1,7,2):"/":strsub(date1,1,4):" ":strsub(date1,9,2):":":strsub(date1,11,2):":":strsub(date1,13,2)
date3 = objecttype("DATE",date2)+10
message(date,date2:@LF:date3)
dt = ObjectClrNew("System.DateTime")
Message("Converted Date: ":date3,TimeFormat(dt.FromOADate(date3),"MM/dd/yyyy hh:mm:ss t"))

Title: Re: date conversion ?
Post by: JTaylor on May 27, 2025, 08:45:23 AM
Misunderstood.  If you wanted to use WB Functions, since you are doing a text conversion anyway, you could change that to create a WB Date and go with WB Functions.

Jim
Title: Re: date conversion ?
Post by: spl on May 27, 2025, 09:47:20 AM
Quote from: JTaylor on May 27, 2025, 08:45:23 AMMisunderstood.  If you wanted to use WB Functions, since you are doing a text conversion anyway, you could change that to create a WB Date and go with WB Functions.

Jim

No, the reason was the date values had to be handled as a timestamp to be placed back into an Oracle db (and I have always hated conversions to Oracle). The conversion was to display the date formatted as text, while placing in db as timestamp. Funny how WB and some other protocols round off to the day not to entire timestamp. Yeah, a little wierd what people ask for.
Title: Re: date conversion ?
Post by: spl on May 27, 2025, 12:55:13 PM
So this is not a big deal... Variables are variable (programming 101). Person who contacted me is a well-versed Python programmer, who inherited an opportunity to look at updating circa 2008 WB code. Was trying to illustrate a quick way to capture text=>datetime, augment and insert into Oracle, and still display text date... But then I updated with this, for fun
;Winbatch 2025A - date conversion test
;Stan Littlefield 5/27/2024
;==================================================================================
types = $"-1=specified name is a function name, reserved word or string constant
0=undefined
1=integer
2=string
4=file handle
32=floating point value
64=binary buffer
128=LPWSTR or Unicode
256=array
512=variant
1024=COM/OLE Object
8192=64-bit integer$"
types = MapCreate(types,'=',@lf)

date="20240524_132134"
date1= strreplace(date,"_","")
date2 = strsub(date1,5,2):"/":strsub(date1,7,2):"/":strsub(date1,1,4):" ":strsub(date1,9,2):":":strsub(date1,11,2):":":strsub(date1,13,2)
date3 = objecttype("DATE",date2)+100
message("Original Date: ":date,"Formatted: ":date2:@LF:"Serialized: ":date3)
dt = ObjectClrNew("System.DateTime")
ddate= dt.FromOADate(date3)
ddate1 = TimeFormat(ddate,"MM/dd/yyyy hh:mm:ss")
Message("Converted Date with VarType: ":date3,ddate:@LF:types[VarType(ddate)]:@LF:ddate1:@LF:types[varType(ddate1)])
;and
Message("Converted Date With Object Type: ":date3,ddate:@LF:ObjectTypeGet(ddate):@LF:ddate1:@LF:types[varType(ddate1)])

Exit
Title: Re: date conversion ?
Post by: td on May 27, 2025, 02:32:29 PM
Why not use

date3 = TimeAdd(objecttype("DATE",date2), "0000:00:100:00:00:00")
instead of

date3 = objecttype("DATE",date2)+100e.t.c.

It saves a trip into .NET Framework landia.
Title: Re: date conversion ?
Post by: spl on May 28, 2025, 03:02:12 AM
Quote from: td on May 27, 2025, 02:32:29 PMWhy not use

date3 = TimeAdd(objecttype("DATE",date2), "0000:00:100:00:00:00")

Nice. Will check if the ask has to return a date var or would string suffice.
Title: Re: date conversion ?
Post by: spl on May 28, 2025, 09:21:41 AM
and this is really,really off-topic. For fun I opened copilot and asked Winbatch to convert text into a date. What I got seemed pretty much what I originally posted. Scraping++ I promise I posted my code before the copilot ask.
Title: Re: date conversion ?
Post by: kdmoyers on May 28, 2025, 01:29:39 PM
wait -- are you saying copilot regurgitated code from earlier in the same thread? 

That's amazing, in multiple ways.
Title: Re: date conversion ?
Post by: td on May 28, 2025, 01:51:58 PM
The big tech-bros are still watching...
Title: Re: date conversion ?
Post by: spl on May 29, 2025, 03:28:36 AM
Quote from: td on May 28, 2025, 01:51:58 PMThe big tech-bros are still watching...

Maybe they will pick up the code for the AI Extender.
Title: Re: date conversion ?
Post by: td on May 29, 2025, 08:25:51 AM
What could possibly go wrong?
Title: Re: date conversion ?
Post by: spl on June 03, 2025, 12:40:39 PM
Quote from: spl on May 28, 2025, 03:02:12 AM
Quote from: td on May 27, 2025, 02:32:29 PMWhy not use

date3 = TimeAdd(objecttype("DATE",date2), "0000:00:100:00:00:00")

Nice. Will check if the ask has to return a date var or would string suffice.

Verdict is in. They want a datetime format. But hey, even AI liked my code.
Title: Re: date conversion ?
Post by: td on June 03, 2025, 09:12:41 PM
Not sure I understand. The date3 variable could easily be converted to a formatted string by TimeFormat, so you must mean something else.