working with dates

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

working with dates

Post by fko » Thu Jun 05, 2014 11:12 am

Hi,

In my databse i have one date in this format: dd/mm/yyyy

and once i make a select to work with this date, i would like to add one variable nDays ( number of days that the user can choose)

And i wrote this:


convert tdate to seconds --tdate is what i have in my database ( in this case 12/06/2014...The format is dd/mm/yyyy )
put nDays * 60 * 60 *24 into daysSeconds
add daysSeconds to tdate
convert fechaFin to system date
answer "fecha " & tdate


And it is not working well...The date shown is not the expected one...:-(
In this case i have that tdate is 01/02/2015...my nDays was 27 and the tdate from the database was 12/06/2014

But if i do this:

put the date into tdate
convert tdate to seconds --SO NOW THE DATE IS NOT COMING FROM MY DATABASE WITH THE FORMAT dd/mm/yyyy
put nDays * 60 * 60 *24 into daysSeconds
add daysSeconds to tdate
convert fechaFin to system date
answer "fecha " & tdate

In this case i have that tdate is 02/07/2014...Because my nDays was 27
it is working well...

Do you know why is acting like this? What am i doing wrong?

regards,
fko

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: working with dates

Post by Klaus » Thu Jun 05, 2014 11:56 am

Hi fko,,

Livecode always presumes a data to be in ENGLISH format: mm/dd/yyyy unless you tell it otherwise.
But in your first script you supplied YOUR data format which was then misinterpreted by Livecode :D

Your date: 12/06/2014
What LC sees: 06/12/2014
So adding 27 days will result in 01/02/2015, as Livecode sees it!

Get the picture?


Best

Klaus

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: working with dates

Post by bangkok » Thu Jun 05, 2014 11:59 am

No need to use seconds.

Code: Select all

   put the date into tDate
   convert tDate to dateitems
   add nDays  to item 3 of tDate
   convert tDate to system date
   answer "fecha " & tDate 
And yes you have to be carefull with the date format (difference between "english", "system" etc.).

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: working with dates

Post by Simon » Thu Jun 05, 2014 12:09 pm

Always best to convert to dateItems, it does all the leap year and weird stuff for you.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

Re: working with dates

Post by fko » Thu Jun 05, 2014 1:16 pm

Hi,

Thanks for all your answers...:-)

Now it is working...

regards,
fko

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

Re: working with dates

Post by fko » Sun Jun 08, 2014 3:20 pm

Hi,

It is me again with the date format...:-(

I have this problem and i am not able to resolve it...:-(

There are some fields and combo box that the user has to fill...And then i am introducing this info in the database (SQLite)...
One of this fields is the date (and in this format dd/mm/yyyy).
So the INSERT INTO is working well and i can see the data in the database as i want ( included the date format).
Then if i made one SELECT is working well too...So i can see for examples the next values of dates: 01/06/2014 or 25/05/2014 or 13/05 /2014...
So i can see the values as i expected.
But then, i am not able to work well with them...:-(

I make one SELECT * FROM MyTable Where name='John'
The value date of what this SELECT returned i put it in the variable tDate

so if i make answer tDate i can see for example 01/06/2014
then i make one convert tDate to system date
And then another convert tDate to dateitems
and if now i make one answer of tDate i have this: 2014,6,1,0,0,0,1
if i add 7 days to this i obtain 2014,6,8,0,0,0,1 --I used this instruction: add 7 to item 3 of tDate

So till this moment it is working as i expected.
But, in the next case the date i obtain from the new SELECT is 25/05/2014...So:

if i make answer tDate i can see for example 25/05/2014
then i make one convert tDate to system date
And then another convert tDate to dateitems
and if now i make one answer of tDate i have this: 25/05/2014 --*****************IS NOT CONVERTING WELL****************
if i add 7 days to this i obtain25/05/2014,,7 --I used this instruction: add 7 to item 3 of tDate


And in the third case the value of date is 02/05/2014
so if i make answer tDate i can see for example 02/05/2014
then i make one convert tDate to system date
And then another convert tDate to dateitems
and if now i make one answer of tDate i have this: 2014,5,2,0,0,0,6 --*********HERE IS MAKING IT CORRECT AGAIN*************

if i add 7 days to this i obtain 2014,5,9,0,0,0,6 --I used this instruction: add 7 to item 3 of tDate

IT IS WORKING WELL ONLY WHEN THE DAY NUMBER HAS A VALUE BETWEEN 1 AND 12 ( As if it is the month )...
But what is strange is that is not taking it as a month, because then is adding correctly the number of days ( 7 in this case ) to the days item...
Because if not in the 01/06/2014, is returning me 08/06/2014 and not adding it to the item '06' ( the month of my date) and not a wrong date ( or at least an unexpected date )...

I hope i explained well my problem with dates...:-)

What am i doing wrong?
Why is not converting well the value from the database (25/05/2014) to dateitems????

regards,
fko

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: working with dates

Post by bn » Sun Jun 08, 2014 4:27 pm

Hi fko,

I use a german system and the german system date is also ddmmyyyy but the delimiter is "." instead of "/". Maybe you change "." to slash in the example.

Code: Select all

on mouseUp
   put "25.06.14" into tDate
   convert tDate from system date to dateItems
   add 7 to item 3 of tDate
   convert tDate from dateItems to system date
   put tDate into field "fRes"
end mouseUp
this works for me.

Could you narrow down your problem in a similar way I did above and post the script that does not work for you?

I have the feeling that your conversion code has a problem.

Kind regards
Bernd

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

Re: working with dates

Post by fko » Sun Jun 08, 2014 5:53 pm

Hi Bernd,

Thanks for your answer: :-)

This is the code...
Since i wrote my doubt in the forum i changed it...So i have no the original one...:-(
Anyway this one is not working...Here i tried to change the variables for each thing i am doing ( convert to system date, convert to dataitems, to see that there is nothing to do with the error...)
So the code can be shorter...:-)


global theDataGridArrayNames
global theDataGridArray
global counterP
put 1 into i
put false into isFinished
put the date into dateToday
convert dateToday to system date

put 1 into indexP
repeat while i <= counterP and isFinished is not true

put the word 2 of theDataGridArray[theDataGridArrayNames[8]["name"]] into nDays
put theDataGridArray[theDataGridArrayNames[3]["name"]] into tDate
convert tDate to system date
put tDate into tDate2
convert tDate2 to dateitems

add nDays to item 3 of tDate2

put tDate2 into finalDate
convert finalDate to system date


if finalDate <= dateToday then
put theDataGridArray[theDataGridArrayNames[1]["name"]] into tdataUsers[indexP]
add 1 to indexP
put empty into tDate
else
end if
if i < counterP then
add 1 to i
else
put true into isFinished
end if
end repeat

I will try what you are telling me to see if it will works...

regards,
fko

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: working with dates

Post by bn » Sun Jun 08, 2014 6:08 pm

Hi fko,

I think the problem is that you first convert the date to system date and then convert that date (in systemDate format) to dateItems.

DateItems need the native date format, not the system date format. The native date format is m/d/yy.
convert tDate to system date -- you convert to system date
put tDate into tDate2
convert tDate2 to dateitems -- you convert a system date to dateItems
Please see in my script how I convert from system date to dateItems

Code: Select all

convert tDate from system date to dateItems
and the other way around

Code: Select all

convert tDate from dateItems to system date
Could you try that?

Kind regards
Bernd

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

Re: working with dates

Post by fko » Sun Jun 08, 2014 6:25 pm

Hi,

I changed it as you told me...

Now it seems that is better of what i had...:-)

But i still have one problem:

in the first case i am trying the date is 12/05/2014...Then i add 7 days...And the date is 19/05/2014

but in my if sentence

"if finalDate <= dateToday then" is not entering...And it should enter...:-(

The second time i pass in the iteration i have 01/06/2014 and adding 7 days i have 08/06/2014...

This comparison (finalDate <= dateToday then) is not correct? Do you think i should pass the values to seconds or it should work?

regards,
fko

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: working with dates

Post by bn » Sun Jun 08, 2014 6:30 pm

Hi fko,
This comparison (finalDate <= dateToday then) is not correct? Do you think i should pass the values to seconds or it should work?
yes, you have to do the comparison on the seconds. Since when comparing just the variables without converting them to seconds LiveCode does a a string comparison and that gives erroneous results. LIvecode does not know that you have dates in your variables.

Kind regards
Bernd

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

Re: working with dates

Post by fko » Sun Jun 08, 2014 7:14 pm

Hi Bernd,

I was testing what you told me...:-)
And now it is working all what you told me except one thing...

I compared the 22/06/2014 with today and it is entering in my condition...And it shouldn't...:-(

I converted like this:

convert tdate from system date to seconds
convert dateToday from system date to seconds

1400450400 Today in seconds
1403388000 22/06/2014

And even with these values it entered in the if sentence...:-(
And i tried it twice...

The conversion to seconds is correct or i made something wrong?

regards,
fko

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: working with dates

Post by bn » Sun Jun 08, 2014 7:22 pm

Hi fko,

your code:
if finalDate <= dateToday then
this tests if the finalDate is SMALLER or EQUAL to dateToday.

If that is what you are looking for then the comparison does not meet your condition. And that seems correct.

If however you want to know if finalDate is BIGGER or EQUAL (or later) I would suggest to use ">="

Kind regards

Bernd

fko
Posts: 61
Joined: Tue May 20, 2014 2:13 pm

Re: working with dates

Post by fko » Sun Jun 08, 2014 7:34 pm

Hi Bernd,

Yes, i am trying to know if the finaldate is smaller os equal than today...
If it is bigger i don't need to do something...Only if it is smaller or equal...
So, for this i used "<="...

Anyway, it should not enter with these values no?

For this i am wondering...:-(

regards,
fko

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: working with dates

Post by bn » Sun Jun 08, 2014 7:53 pm

Hi fko,

I tested this and it works:

Code: Select all

on mouseUp
   put the date into tDate
   convert tDate to system date -- just to convert it
   convert tDate from system date to dateItems
   put tDate into tToday -- put tDate as dateItems into tToday
   add 7 to item 3 of tDate -- tDate is now the date in the future
   convert tDate to seconds
   convert tToday from dateItems to seconds
   if tDate <= tToday then 
      answer "date is past"
   else
      answer "date is coming "
   end if
end mouseUp
please try to reduce in a new stack your problem to the bare minimum. Leave out all the dateGrid stuff and so on. Just replicate the core structure of the problem. And then post your code that does not work. That way it is a lot easier to track down funny behavior. Now I don't know when you convert what into what and when before the conditional.

Kind regards
Bernd

Post Reply