LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Scan from String Issue

Solved!
Go to solution

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???

 Screen Shot 06-07-18 at 09.39 AM.PNG

0 Kudos
Message 1 of 11
(4,337 Views)

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'.

 

Message 2 of 11
(4,317 Views)
Solution
Accepted by topic author dbrown78

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.

0 Kudos
Message 3 of 11
(4,306 Views)

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.

0 Kudos
Message 4 of 11
(4,305 Views)

good to know.  I didn't realize that.  

Let me play with it and see if I can get it to work

0 Kudos
Message 5 of 11
(4,300 Views)

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!

0 Kudos
Message 6 of 11
(4,297 Views)

This should work. Smiley Happy

It replaces the '-' with a space and adds 2000 to the year.

0 Kudos
Message 7 of 11
(4,296 Views)

Using your original VI and changing the search and replace to use " " space characters works too. Like NeilR said, you'll need to add 2000 to the year to get it to work with the follow-up subVI.

 

Edit: Don't open a bunch of pages, then come back to one 20 minutes later and fail to refresh...


GCentral
0 Kudos
Message 8 of 11
(4,279 Views)
Solution
Accepted by topic author dbrown78

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:

String to TimeStamp.png

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  

0 Kudos
Message 9 of 11
(4,276 Views)
Solution
Accepted by topic author dbrown78

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.

0 Kudos
Message 10 of 11
(4,270 Views)