VishalMistry
30th October 2015, 16:58
Hi all,
I have a string record as below:
record = "vishal|10|10/12/2015"
i want to scan the variable record store individual values.
I know how to scan string and numerics but there is no format for scanning date value.
Can anybody guide please how to scan date value (just plain date and date + time value) ?
Vishal
bhushanchanda
30th October 2015, 17:41
Hi,
You can do it with string.scan(). You just need to define what format will the string have.
e.g. In the case mentioned i.e. "vishal|10|10/12/2015"
string var(100)
string name(50)
long position,day,month,year
var = "vishal|10|10/12/2015"
string.scan(var, "%s|%d|%d/%d/%d",name,position,day,month,year)
Now all the variables will have their values. You can use num.to.date() to convert it to Date and date.to.utc() to convert into UTC Date/Time.
mark_h
30th October 2015, 19:39
Hmmm - shouldn't that be date.to.num where you give the month, day and year to create a date. Versus num.to.date - which takes a date and breaks it into year, month and day of the month. :)
I never even thought of using two different separators in the string scan. Could have save myself a few lines of code.:)
vamsi_gujjula
30th October 2015, 19:52
infact MArk .. i had this requirement to scan the date , as per locale some people use '/' or '-' .. to tackle this used a variable..as well :p
mark_h
30th October 2015, 21:24
infact MArk .. i had this requirement to scan the date , as per locale some people use '/' or '-' .. to tackle this used a variable..as well :p
And another idea I did not think of - using a variable in the string scan for format. We always told the customer what to use. Of course I can't say for sure I did not try a few different things 15 years ago - and now we just copy code and move on. :)