date conversion ?

Started by spl, May 26, 2025, 12:52:07 PM

Previous topic - Next topic

spl

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)
Stan - formerly stanl [ex-Pundit]

JTaylor


spl

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"))

Stan - formerly stanl [ex-Pundit]

JTaylor

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

spl

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.
Stan - formerly stanl [ex-Pundit]

spl

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
Stan - formerly stanl [ex-Pundit]

td

Why not use

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

instead of

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

It saves a trip into .NET Framework landia.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

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.
Stan - formerly stanl [ex-Pundit]

spl

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.
Stan - formerly stanl [ex-Pundit]

kdmoyers

wait -- are you saying copilot regurgitated code from earlier in the same thread? 

That's amazing, in multiple ways.
The mind is everything; What you think, you become.

td

The big tech-bros are still watching...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

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.
Stan - formerly stanl [ex-Pundit]

td

What could possibly go wrong?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

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.
Stan - formerly stanl [ex-Pundit]

td

Not sure I understand. The date3 variable could easily be converted to a formatted string by TimeFormat, so you must mean something else.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

SMF spam blocked by CleanTalk