06-07-2018 08:47 AM - edited 06-07-2018 08:48 AM
Good morning. I seem to be having a little difficulty converting a string into a timestamp. I'm good in all of the places I need to do this conversion but 1. The date I'm being fed is of the form 20-MAY-18. I connected this to a scan from string with the %d-%s-%d, but the %s is coming back as MAY-18 and the last number is coming up as 0. I thought maybe it had something to do with my delimiters, so I changed them to / instead of -, but it's still giving me MAY/18. Any one have any suggestions on how to get this to cooperate??? Am I doing something wrong???
Solved! Go to Solution.
06-07-2018 09:11 AM - edited 06-07-2018 09:12 AM
You have to be careful with the delimiter characters used. A '-' for instance is seen as a left justify indicator when scanned from the string. Also the 18 needs to be 2018 to be interpreted as a 'year'.
06-07-2018 09:20 AM
The "%s" format specifier will match any non-whitespace characters, including a dash or slash, and only stops when it reaches a whitespace character. If you want to limit it to only matching capital letters, you can use "%[A-Z]", then it will stop when it reaches the dash or slash.
06-07-2018 09:20 AM
Well, the problem is, I have no control on how the date is being fed to me. As you can see from the case structures, it is being given to me in 2 different formats. Either DD-MMM-YY or mm/dd/yy hh:mm:ss.000
I figured the 18 vs 2018 would be an issue.... Just haven't gotten there yet to fix it because I can't get beyond the scan from string interpreting my delimiters correctly. I did find the - being used for text justification, that's one reason i have the code changing it to / instead. I just can't figure out why the string bit is returning MAY/18 vs MAY.
06-07-2018 09:22 AM
good to know. I didn't realize that.
Let me play with it and see if I can get it to work
06-07-2018 09:24 AM
yup, that did it. Instead of replacing the hyphen with a /, I replaced them with a space instead and it is now working. Thanks for your help!
06-07-2018 09:24 AM
This should work.
It replaces the '-' with a space and adds 2000 to the year.
06-07-2018 09:44 AM - edited 06-07-2018 09:45 AM
06-07-2018 09:48 AM
Once I figured out how to use Scan from String, I use it for all of my "Scan from String" problems. You have a string, "20 May 2018", and you want to convert it to a TimeStamp. Drop down a Scan from String, wire "20 MAY 2018" into the String Input, and wire a TimeStamp constant into the Default Value 1 slot. Be sure to wire Error Out to allow the function to warn you when you try to convert "20 Mabel 2018" ...
Now the only thing missing is the Format String, which (by default) is "%T", i.e. the generic Absolute Time format. But you have a specific Time format -- a "day of month" (%d) separated by a space from a "Full Month Name" (%B), though this might also be an "Abbreviated Month Name" (%b), again separated by a space from a 4-digit year (%Y). This expanded Format String is written %<%d %B %Y>T and is shown in this Snippet:
If there is more before or after this String, just add appropriate default Input types and expand the Format String to match. If the TimeStamp also includes Time, the appropriate format specifications for them can be placed inside the Format String illustrated above. To see the (bewildering) list of TimeStamp Formats, search Help for "Format Codes for the Time Format String".
Bob Schor
06-07-2018 09:57 AM
Actually Bob, if you replace the format string with %<%d-%B-%y>T then the required format "20-May-18" is converted perfectly !! Nice one.