How Do I Find Number of Days between 2 Dates? - Solved
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How Do I Find Number of Days between 2 Dates? - Solved
How Do I Find Number of Days between 2 Dates?
So I have been beating my head against a wall for almost 3 hours, trying not to bother anyone on the forum. I looked at several instructions in the dictionary, but I have not found a way to get the number of days between 2 different dates.
Thanks,
David
So I have been beating my head against a wall for almost 3 hours, trying not to bother anyone on the forum. I looked at several instructions in the dictionary, but I have not found a way to get the number of days between 2 different dates.
Thanks,
David
Last edited by DR White on Fri Jan 27, 2017 3:44 pm, edited 1 time in total.
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: How Do I Find Number of Days between 2 Dates?
ftp://ftp.bu.edu/pub/mirrors/info-mac/_ ... -dates.hqx
You'll need a Macintosh capable of running Mac OS 9 or earlier: I'm going to have a poke
at it on my iMac G5.
You'll need a Macintosh capable of running Mac OS 9 or earlier: I'm going to have a poke
at it on my iMac G5.
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: How Do I Find Number of Days between 2 Dates?
Short answer:
Long answer:
a) See Richmond's link above
b) See https://www.youtube.com/watch?v=-5wpm-gesOY
In short, you will find circumstances in which the function above will fail. Time is surprisingly complicated stuff. But most of the time it'll do what you need.
Code: Select all
on mouseUp
put DaysBetween("12/1/16", the date)
end mouseUp
function DaysBetween pDate1, pDate2
-- Make sure we're working with valid date values:
if pDate1 is not a date then
return "First argument is not a date" for error
end if
if pDate2 is not a date then
return "Second argument is not a date" for error
end if
-- Do the arithmetic with integer seconds:
convert pDate1 to seconds
convert pDate2 to seconds
put abs(pDate1-pDate2) into tDiff
-- Convert seconds to days and return it:
return (tDiff / (60*60*24))
end DaysBetween
a) See Richmond's link above
b) See https://www.youtube.com/watch?v=-5wpm-gesOY
In short, you will find circumstances in which the function above will fail. Time is surprisingly complicated stuff. But most of the time it'll do what you need.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: How Do I Find Number of Days between 2 Dates?
Ouch: Hypercard behaving oddly under Mac OS Tiger (10.4) on my G5:
I wonder if the stack is on the different disk to the operating system?
I wonder if the stack is on the different disk to the operating system?
-
- VIP Livecode Opensource Backer
- Posts: 366
- Joined: Mon Jun 10, 2013 1:32 pm
Re: How Do I Find Number of Days between 2 Dates?
Hi
When I started using livecode the first thing I noticed was the lack of date functions I was used to in Foxpro - Yes I can write them using judicious use of date convert etc but I like my simple Foxpro calls. So the first library I wrote was date routines I had been using for the best part of 25 years.
With the seconds/zones/GMT problem I decided that I needed a reliable way to do the dates between dates routine. So I wrote a julian day number calculation base on a wikipedia article
With that out of the way the days between function became trivial
Some of the other routines are trivial but very useful nonetheless if you have been using them for over 25 years
Another one used in an employee database
GOMONTH() is a stock Foxpro function but GODAYS() was needed because in foxpro we can just add numbers to a date variable and it returns a day variable
-- All my routines are UK date specific -
I'm sure we can make these smaller faster more beautiful - but they were written while i was learning and they work for me -
When I started using livecode the first thing I noticed was the lack of date functions I was used to in Foxpro - Yes I can write them using judicious use of date convert etc but I like my simple Foxpro calls. So the first library I wrote was date routines I had been using for the best part of 25 years.
With the seconds/zones/GMT problem I decided that I needed a reliable way to do the dates between dates routine. So I wrote a julian day number calculation base on a wikipedia article
Code: Select all
-- Julian Day Number -- As Per http://fox.wikis.com/wc.dll?Wiki~JulianDates
-- Agrees with Wikipedia January 1, 2000, was 2,451,545.
function JDN pDate
local lnD, lnM, lnYr, lnJulianDate
set itemdelimiter to "/"
put item 1 of pDate into lnD
put item 2 of pDate into lnM
put item 3 of pDate into lnYr
put 367 * lnYr - TRUNC(7 * (lnYr + TRUNC ((lnM + 9) / 12)) / 4) into lnJulianDate
put lnJulianDate - TRUNC(3 * (TRUNC((lnYr + (lnM - 9) / 7) / 100) + 1) / 4) into lnJulianDate
put lnJulianDate + TRUNC(275 * lnM / 9) + lnD + 1721029 into lnJulianDate
Return lnJulianDate
end JDN
Code: Select all
-- Function : DayBetween()
-- Remarks : If the second date is blank it will use the current systemdate
-- : Assumes caller knows which is before or after
-- Params : {Date} pDate1, {Date} pDate2
-- Returns : {Integer} Absolute number of days (with decimals) between the two dates
Function DaysBetween pDate1, Pdate2
local lnD1, lnD2, lnDays
set the usesystemdate to true
if pDate2 is empty then
put the system date into pDate2
end if
put JDN(pDate1) into lnD1
put JDN(pDate2) into lnD2
put ABS(lnD1 - lnD2) into lnDays
return lnDays
end DaysBetween
Code: Select all
-- Function : GODAYS()
-- Remarks : The date passed in as a parameter plus or minus the days specified.
-- param : {Date} pDate - Date we are adding or subtracting to/from
-- param : {Integer} pDays - Number of days to add/substract to pDate
-- Returns : {Date} pdDate - Date pDays forward or back
function GODAYS pDate, pDays
set useSystemDate to true
convert pDate to dateitems
add pDays to item 3 of pDate
convert pDate to system date -- NOt strictly needed but ....
return pDate
end GODAYS
Code: Select all
-- Function : Age()
-- Remarks : Pass in a Date of Birth to Find age in years months and Days
-- : Uses Julian Number
-- Params : {Date} pDate1, {Date} pDate2
-- Returns : {Integer} Absolute number of days (with decimals) between the two dates
Function Age pDOB, pUpto
local lnD1, lnD2, lnYears, lnMonths, ldToday, lnDays, lcAge
set itemdelimiter to "/"
put the short system date into ldToday
-- Allow age calcuklation upto any date - even future
if pUpto is not empty then
put pUpto into ldToday
end if
-- Get the Julian date with base January 1st 4713 BC
put JDN(ldToday) into lnD1
put JDN(pDOB) into lnD2
put TRUNC((lnD1 - lnD2) / 365.25) into lnYears
put TRUNC((((lnD1 - lnD2) / 365.25) - lnYears) * 12) into lnMonths
put TRUNC( ( ((((lnD1 - lnD2) / 365.25) - lnYears) * 12) - lnMonths) * 30.42) into lnDays -- My addition which makes it as close as dammit for the days
-- Send back 3 ITEMS
put lnYears & "," & lnMonths & "," & lnDays into lcAge
return lcAge
end Age
-- All my routines are UK date specific -
"We don't need no steenkin timezones!"

Code: Select all
-- Function : GOMONTH()
-- Remarks : The date passed in as a parameter plus or minus
-- : the months specified. To go forward a year pass 12 back a year -12
-- param : {Date} pDate - Base Date to add or substract the months to
-- param : {integer} pnMonths - Number of months to add/substract to pDate
-- Returns : {date} pDate - Date nMonths forward or -nMonths back
function GOMONTH pDate, pMonths
-- We are passing in a string in BRITISH (UK) format not ENGISH (AMERICAN)
-- So we tell the system that (assuming the users date is in DD/MM/YYY format
-- This property is local and only lasts for the current handler
set useSystemDate to true
convert pDate to dateitems
add pMonths to item 2 of pDate
convert pDate to system date
return pDate
end GOMONTH
-- Function : GODAYS()
-- Remarks : The date passed in as a parameter plus or minus the days specified.
-- param : {Date} pDate - Date we are adding or subtracting to/from
-- param : {Integer} pDays - Number of days to add/substract to pDate
-- Returns : {Date} pdDate - Date pDays forward or back
function GODAYS pDate, pDays
set useSystemDate to true
convert pDate to dateitems
add pDays to item 3 of pDate
convert pDate to system date -- Not strictly needed but ....
return pDate
end GODAYS
Regards Lagi"If it ain't broke , don't fix it"
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: How Do I Find Number of Days between 2 Dates?
Good use of useSystemDate to handle non-US date formats. My function should have included that. Good catch.
FWIW, IIRC when converting to or from seconds LC takes into account the local system's GMT offset per RFC 2822, so "the seconds" is always GMT and conversions should work independent of time zones.
The only other date format in LC that preserves GMT offset is "the internet date", but while it's great for logging and such it's often too long for display to the user and more cumbersome to parse than other formats it can be easily converted to.
FWIW, IIRC when converting to or from seconds LC takes into account the local system's GMT offset per RFC 2822, so "the seconds" is always GMT and conversions should work independent of time zones.
The only other date format in LC that preserves GMT offset is "the internet date", but while it's great for logging and such it's often too long for display to the user and more cumbersome to parse than other formats it can be easily converted to.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: How Do I Find Number of Days between 2 Dates?
North American date format: MM/DD/YY
so this is 11th December 2016 and
NOT 12th November 2016.
Re: How Do I Find Number of Days between 2 Dates?
Very very easy:DR White wrote:How Do I Find Number of Days between 2 Dates?
So I have been beating my head against a wall for almost 3 hours, trying not to bother anyone on the forum. I looked at several instructions in the dictionary, but I have not found a way to get the number of days between 2 different dates.
Thanks,
David
Code: Select all
put libDate_DaysBetween( pYear1,pMonth1,pDay1,pYear2,pMonth2,pDay2)
Propose changes on https://github.com/angerangel/libdate/b ... ate.script
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: How Do I Find Number of Days between 2 Dates?
Max,
Thanks for guiding me to this library. I will give it a try latter. I have coded a work-around currently.
Thanks again for your help,
David
Thanks for guiding me to this library. I will give it a try latter. I have coded a work-around currently.
Thanks again for your help,
David