• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2932
  • Last Modified:

Parse string into Datetime Visual Fox Pro 6.0

Hello Experts,

I have a string where its a Datetime and I need to parse in a FoxPro datetime format.

ReturnString= 1009291928
i = 0
Year=SUBSTR(ReturnString,i+1,2)  result 10
Month=SUBSTR(ReturnString,i+3,2)  result 09
Day=SUBSTR(ReturnString,i+5,2)  result 29
Hour=SUBSTR(ReturnString,i+7,2)  result 19
Min=SUBSTR(ReturnString,i+9,2)  result 28

I need it in FoxPro Datetime format.

Thanks.
0
ASPDEV
Asked:
ASPDEV
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
pcelbaCommented:
DATETIME() function should help:


FUNCTION STR2DateTime
LPARAMETER ReturnString

LOCAL i, Year, Month, Day, Hour, Min
i = 0
Year=VAL(SUBSTR(m.ReturnString,m.i+1,2)) && result 10
Month=VAL(SUBSTR(m.ReturnString,m.i+3,2))&& result 09
Day=VAL(SUBSTR(m.ReturnString,m.i+5,2))  && result 29
Hour=VAL(SUBSTR(m.ReturnString,m.i+7,2)) && result 19
Min=VAL(SUBSTR(m.ReturnString,m.i+9,2))  && result 28

RETURN DATETIME(SET('CENTURY', 1)*100 + IIF(m.Year < SET('CENTURY', 2), 100, 0), m.Month, m.Day, m.Hour, m.Min)

Open in new window

0
 
AndrewJenCommented:
ReturnString= "1009291928"
i = 0
Year=SUBSTR(ReturnString,i+1,2) && result 10
Month=SUBSTR(ReturnString,i+3,2) &&  result 09
Day=SUBSTR(ReturnString,i+5,2) && result 29
Hour=SUBSTR(ReturnString,i+7,2) && result 19
Min=SUBSTR(ReturnString,i+9,2) && result 28
* CTOT("2000-10-24T13:30:00")
mydatetime= CTOT("20"+year+"-"+month+"-"+day+"T"+hour+":"+min+":00")
? mydatetime
0
 
pcelbaCommented:
The function should use SET CENTURY ... ROLLOVER ....  setting to calculate the century part of the date.

And it contains a small bug, sorry.

RETURN DATETIME(m.Year + SET('CENTURY', 1)*100 + IIF(m.Year < SET('CENTURY', 2), 100, 0), m.Month, m.Day, m.Hour, m.Min)

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ASPDEVAuthor Commented:
pcelba & AndrewJen,

I tried both of your code, it didn't work.

Thanks.
0
 
jrbbldrCommented:
When you say:  "I tried both of your code, it didn't work."

How?  
Specifically what did not work?
  *  Did it throw an error message?
  *  Were the results just not as you expect?

With better clarity/detail, we can assist you better.

Good Luck


0
 
jrbbldrCommented:
The VFP function CTOT()  will convert a string of characters (if formatted correctly) into a DateTime value.

You should easily be able to take the individual memory variables:
       Year=SUBSTR(ReturnString,i+1,2)  result 10
       Month=SUBSTR(ReturnString,i+3,2)  result 09
       Day=SUBSTR(ReturnString,i+5,2)  result 29
       Hour=SUBSTR(ReturnString,i+7,2)  result 19
       Min=SUBSTR(ReturnString,i+9,2)  result 28
and put them into a properly formatted string and then run the function to get a DateTime value returned.

Look at your VFP Help for CTOT()

Good Luck
0
 
Olaf DoschkeSoftware DeveloperCommented:
Pavel Celba's function returns the wrong year, true. He forgot to add the m.year to the year portion.

AndrewJen's Code works, but CTOD is depending on environment settings.

But you might have another error or it doesn't do anything to you, if you just execute pcelbas code as is, nothing happens, as it's just a function definition, you need to call it, eg STR2DateTime('1009291928').

Therefore jrbbldrs question is very valid: How do you call this? How is this embedded in your code?

I'll throw in another solution making use of the invariantly valid format {^YYYY-MM-DD hh:mm:ss}:

ReturnString='1009291928'
? EVAL('{^ 20'+TRANSFORM(m.ReturnString,'@R 99-99-99 99:99:00')+'}')

As you only have two digits for the year you'd need to also take care if putting that into 20XX is okay, or if you also need dates in the year 19XX if eg. XX>80 or year 21XX if eg XX<10.

Bye, Olaf.
0
 
pcelbaCommented:
"Pavel Celba's function returns the wrong year, true."

It seems you missed my fix posted five minutes later. :-)

0
 
Olaf DoschkeSoftware DeveloperCommented:
Yes, I missed your fix. Maybe ASPDEV also did so.

Bye, Olaf.
0
 
ASPDEVAuthor Commented:
I made a mistake in my query earlier.

Thanks.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now