› Clovertech Forums › Cloverleaf › Date format
Hello I am looking for some assistance on a date format
I have a date coming in on 1(0).0(0).PID(0).#7(0).[0]
its coming in as 19990101
I need to change it to month day year and add /
so 01/01/1999
Thank you
set myDate [lindex $xlateInVals 0]
set xlateOutVals [clock format $myDate -format “%m/%d/%Y”]
Ha! forgot to copy and paste the clock scan command… Looks like Tipu gave you the more “complete” answer. Thanks 😉
If in an Xlate and no Tcl:
Use the STRING Action substring function to extract the ccyy, mm, and dd elements to temp variables.
Then use a CONCAT Action to combine the respective temp variables using / as the separator.
If you are going to use Tcl don’t forget the xlateOut lists (vals, etc) are LISTS and should be treated as such.
email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.
If you want a one-liner and a more thorough attempt, it will be like so:
lset xlateOutVals 0 [clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y”]
You really should tell TCL how the incoming date is formatted, otherwise it’ll take a best guess which may result in incorrect output data and/or just error entirely.
Hello I tired your solution and received this error
MESSAGE 1
Tcl callout error
lset xlateOutVals 0 [clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y”]:
errorCode: NONE
errorInfo:
bad switch “-format”: must be -base or -gmt
while executing
“clock scan [lindex $xlateInVals 0] -format “%Y%d%m””
invoked from within
“clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y””
invoked from within
“lset xlateOutVals 0 [clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y”]”
<End of errorInfo>
Interesting. Works when I try it.
Which version of TCL are you using? ‘info patchlevel’ command should tell you.
I think the format option was added in 8.5
I also noticed a mix up in the original code snippet. I’m assuming the date is coming in the following format: YYYYMMDD and you want it turned into MM/DD/YYYY
Updated snippet below:
lset xlateOutVals 0 [clock format [clock scan [lindex $xlateInVals 0] -format %Y%m%d] -format %m/%d/%Y]
I am still using cloverleaf 5.8 so not sure what version that is using
Makes sense. Your version of TCL is too old.
You can try this. It’s definitely not ideal. It’s just manipulating the string by extracting the YEAR, MONTH, DAY and creating a new string MM/DD/YYYY
Hopefully it’ll work with your version of TCL
Incoming string format YYYYMMDD
Output string format MM/DD/YYYY
lset xlateOutVals 0 [string cat [string range [lindex $xlateInVals 0] 4 5] “/” [string range [lindex $xlateInVals 0] 6 7] “/” [string range [lindex $xlateInVals 0] 0 3] ]
Thank you so much for the help but I still got another error, I am sure do to my old tcl
MESSAGE 1
Tcl callout error
lset xlateOutVals 0 [string cat [string range [lindex $xlateInVals 0] 4 5] “/” [string range [lindex $xlateInVals 0] 6 7] “/” [string range [lindex $xlateInVals 0] 0 3] ]:
errorCode: NONE
errorInfo:
bad option “cat”: must be bytelength, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart
while executing
“string cat[string range [lindex $xlateInVals 0] 4 5] “/” [string range [lindex $xlateInVals 0] 6 7] “/” [string range [lindex $xlateInVals 0] …”
invoked from within
“lset xlateOutVals 0 [string cat[string range [lindex $xlateInVals 0] 4 5] “/” [string range [lindex $xlateInVals 0] 6 7] “/” [string range [li…”
<End of errorInfo>
Hmmm
Try this
lset xlateOutVals 0 “[string range [lindex $xlateInVals 0] 4 5]/[string range [lindex $xlateInVals 0] 6 7]/[string range [lindex $xlateInVals 0] 0 3]”
You are awesome!!
Thank you so much for the assistance!!
No problem! Glad it worked!
you can do this with a code snippet in the translate. couple of different ways – first is to string range each portion out, then rebuild it with the slashes. Second way is to use clock scan/clock format.
clock scan example
lassign $xlateInVals in_pid_7_0
set out_pid_7_0 [clock format [clock scan $in_val -format %Y%m%d] -format %m/%d/%Y]
set xlateOutVals
string range example
lassign $xlateInVals in_pid_7_0
set out_pid_7_0 “[string range $in_pid_7_0 4 5]/[string range $in_pid_7_0 6 7]/[string range $in_pid_7_0 0 3]”
set xlateOutVals
formatting is weird – removed my “list $out_pid_7_0” within brackets after the “set XlateOutVals”
Paul Bishop
Carle Foundation Hospital
Urbana, IL
I am still using cloverleaf 5.8
Hello I tried Tipu’s solution but I received the below error
MESSAGE 1
Tcl callout error
lset xlateOutVals 0 [clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y”]:
errorCode: NONE
errorInfo:
bad switch “-format”: must be -base or -gmt
while executing
“clock scan [lindex $xlateInVals 0] -format “%Y%d%m””
invoked from within
“clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y””
invoked from within
“lset xlateOutVals 0 [clock format [clock scan [lindex $xlateInVals 0] -format “%Y%d%m”] -format “%m/%d/%Y”]”
<End of errorInfo>
Why jump through the hoops of tearing the string apart and then put it back together? The clock command was designed for cases like this. Boris was on the right track but his command was incomplete.
Try
set myDate [lindex $xlateInVals 0]
set xlateOutVals
-format “%m/%d/%Y”]]