Normal Topic Change a string to a date (Read 1312 times)
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Change a string to a date
Jun 7th, 2017 at 7:17pm
Print Post Print Post  
I have the ability to get a string that signifies a date, such as "061117" would be June 11, 2017.

I want to change that to a date and then compare that to today's date.

What is the easiest way to convert my string information to a date format?

I'd appreciate any suggestions!

NHUser
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Change a string to a date
Reply #1 - Jun 7th, 2017 at 8:19pm
Print Post Print Post  
Hi Paul,

The code below splits apart the 6 digit string representing a date and puts slashes into it which @ToDate then converts to a date variable.

Code
Select All
Var vS as String
Var vD as Date

	vS = "061117"
	vD = @ToDate(@Left(vS, 2) + "/" + @Mid(vS, 3, 2) + "/" +  @Mid(vS, 5, 2))
	Writeln(vD) 



-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: Change a string to a date
Reply #2 - Jun 9th, 2017 at 2:59pm
Print Post Print Post  
Thanks, I've got the date thing working.  Now I've run into another problem.

I'm trying to compare today's date to another date based on an integer that defines the shelf life of the material.  I'm using the equation below, where vTime is an integer, vD is the calculated date per your programming and vMat is the number of days of available shelf life.

The formula works as long as I'm within the shelf life of the material (when vTime would be greater than zero), but when the number should go negative (ie, one day beyond the shelf life) I don't get a negative number.  I get a number over 10 million!

Is this normal?  I can work with a hugh number, but I want to make sure this will happen consistently.

Please advise.

vTime=vD-(@Date-vMat)
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Change a string to a date
Reply #3 - Jun 9th, 2017 at 3:37pm
Print Post Print Post  
Hi Paul,

I'll lead you through what is happening and why you are getting that large number instead of the negative you are expecting.

For this I will use the following values
vMat = 35
@Date = 6/9/2017
vD = 5/3/2017

With those values put in to the equation you are looking at

vTime=5/3/2017 - (6/9/2017 - 35)

And we start solving it

vTime=5/3/2017 - (5/5/2017)

Now a Date minus another Date results in a date in this case it is going to result as 4083/12/29(Cause there is no such thing as a negative date value and it's wrapping back around)

vTime = 4083/12/29

And since you are assigning that date value to an Int you get

1491283

But that is not at all what you want. You want the number of days. So you want to do the math on the number of days.

So you want

Code
Select All
vTime=@ToNumber(vD)-@ToNumber(@Date-vMat) 



Which gives you

vTime= @ToNumber(5/3/2017) - @ToNumber(6/9/2017 - 35)
vTime= @ToNumber(5/3/2017) - @ToNumber(5/5/2017)
vTime = 736452 - 736454
vTime = -2

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: Change a string to a date
Reply #4 - Jun 9th, 2017 at 5:46pm
Print Post Print Post  
Thanks!

I thought a date minus a date would provide the number of days between the two dates.

Anyway, I used your formula and it's working great!

Thanks for the help!
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Change a string to a date
Reply #5 - Jun 9th, 2017 at 7:39pm
Print Post Print Post  
Hi Paul,

There is an entire table inside the code for Sbasic that determines output type of calculations based on the variable types of the two variables involved. In general, any one type plus/minus/divided by/multiplied by that same type returns that same type. So Int plus Int obviously returns Int. Same thing for Date, Time, Money, etc.

The reason I say in general is there are exceptions like String - String is not allowed as SBasic can not determine what "Bob" - "Banana" would be.(Obviously the answer is "Hungry" but I digress Grin )

So if you find yourself not getting the answer you want out of an equation, look at the types of your variables involved and see if perhaps you don't need to change one to a different type using one of the @To***() commands. One way to determine if you need to do this(It's actually the way I did it to get the numbers/dates for the previous post) is to use Writeln() to write out the result of each separate calculation. So say you have the equation of A = B + (B/(C-D)) I would fill that section up with debug like:
Code
Select All
Writeln(C-D)
Writeln(B/(C-D))
Writeln(B + (B/(C-D)))
A = B + (B/(C-D))
Writeln(A) 


This way you can see what each part of the equation is returning.

This is also why sometimes you'll see someone perform an operation on several lines when it could be crammed into one line like the above statement. They might write that same piece of code as
Code
Select All
Var vTemp as WhateverTypeOfVariableTheyNeed
vTemp= C-D
vTemp = B/vTemp
vTemp = B+vTemp
A = vTemp 


This allows them to write out vTemp at several points along the way and see where the error is coming from. Yes it takes up a few more lines but it's easier to troubleshoot then a line like
Code
Select All
PrintString(@AsFormattedByLE(Total, 0, vSubTotal + (vSubTotal * (vTaxRate / 100)) + vShipping + vHandling + vFees + @XLookup(@FN, CustomerID, "Customers!ID", "Discount")), 650, vPageY, 100, "Arial", 16, 0, 1) 



Note: I'm not saying you should have each operation on it's own line but sometimes building a large equation in pieces will save you a lot of time when/if something goes wrong with it.

Have a great weekend,
-Ray

P.S. I apologize for such a long post but hopefully others will find pieces of it useful in the future.
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged