## Working With Dates < 1/1/1970

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

TerryL
Posts: 69
Joined: Sat Nov 23, 2013 8:57 pm

### Working With Dates < 1/1/1970

Convert to seconds/dateItems and is a date don't work in Windows when calculating with dates < 1/1/1970. This is a Windows issue, not LiveCode. A work-around is to do math directly on the date numbers with approximate result.

I have a kludgy past-date function. There must be a better elegant solution. Terry

Code: Select all

``````on mouseUp  --age
set the itemDel to "/"
repeat forever
ask "If Elvis were alive, how old would he be? (mo/dy/year)" with "1/8/1935" titled "Age"
if it = empty then exit to top
if item 1 of it is a number and item 1 of it >= "1" and item 1 of it <= "12" and \
item 2 of it is a number and item 2 of it >= "1" and item 2 of it <= "31" and \
item 3 of it is a number and length(item 3 of it) = "4" then
set the itemDel to ","  --default
exit repeat
end if
beep
answer "Please enter date as: MONTH 1-12 / DAY 1-31 / 4-digit YEAR" with "OK" titled "Age"
end repeat
answer "Birthdate:" && it &cr& "Age:" && age(it) & "." with "OK" titled "Age"
end mouseUp

function age Birthdate  --age("mo/dy/year")  [4-digit year] [handles windows dates < 1/1/1970]
convert date() to dateItems
put item 1 of it into NewerYear
put item 2 of it into NewerMonth
put item 3 of it into NewerDay
set the itemDel to "/"
put item 1 of Birthdate into OlderMonth
put item 2 of Birthdate into OlderDay
put item 3 of Birthdate into OlderYear
set the itemDel to ","  --default
put NewerYear - OlderYear into AgeYear
if NewerMonth < OlderMonth or NewerMonth = OlderMonth and NewerDay < OlderDay then subtract 1 from AgeYear
put NewerMonth - OlderMonth into AgeMonth
if AgeMonth < "0" or NewerDay < OlderDay then add 12 to AgeMonth
put NewerDay - OlderDay into AgeDay
if AgeDay < "0" then
subtract 1 from AgeMonth
end if
return AgeYear &&"years,"&& AgeMonth &&"months,"&& AgeDay &&"days"
end age
``````
(Edit1: added date validation to test user input.)
Last edited by TerryL on Wed Apr 15, 2015 6:28 pm, edited 1 time in total.
Beginner Lab (LiveCode tutorial) and StarterKit (my public stacks)
https://tlittle72.neocities.org/info.html#26Anchor

dunbarx
VIP Livecode Opensource Backer
Posts: 6918
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Working With Dates < 1/1/1970

Windows? What is that?

On a Mac, the baseline date is just that, a baseLine. It is not the big bang. If you:

Code: Select all

``````on mouseUp
put "1/1/1955" into temp
convert temp to  dateItems
convert temp to seconds
end mouseUp``````
You get a valid date representation and then a negative number (which can be used just like any other number). What does windows do?

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
Posts: 7930
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

### Re: Working With Dates < 1/1/1970

It's a limitation/bug in Windows - checking "the result" after attempting the "convert" command on a string that evaluates to a date prior to 1/1/70 results in "invalid date".
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com

SparkOut
Posts: 2286
Joined: Sun Sep 23, 2007 4:58 pm

### Re: Working With Dates < 1/1/1970

Is it accurate to say a "limitation/bug in Windows" or better "a limitation/bug in the way LiveCode has been made to interact with the implementation of date functions native to Windows"?
I find it hard to see how this long-standing issue has been not been addressed in the engine.Other applications manage fine. Sure we can roll our own date functions, or use Malte's excellent library, but this problem should be dealt with. Otherwise, what is the point in having any native functions? Especially in a product meant to be cross-platform.

FourthWorld
VIP Livecode Opensource Backer
Posts: 7930
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

### Re: Working With Dates < 1/1/1970

Agreed: even if it is a bug in Windows, if it can be worked around in script it would be even easier for the engine to work around it for us.