1. DaysBetweenDates - the easy way with Euphoria 4.0+

-------------------------------------------------------------------------------------------------- 
--  add the following function to the bottom of OpenEuphoria 4.0+ std/datetime.e 
public function DaysBetweenDates(datetime d1, datetime d2) 
return floor((datetimeToSeconds(d2) - datetimeToSeconds(d1))/DayLengthInSeconds) 
end function 
--------------------------------------------------------------------------------------------------
-- test_DaysBetweenDates.ex 
 
include std/datetime.e as dt 
 
constant BIRTH_DATE = new(1950, 6, 28) 
constant DATE2 = now() 
 
constant n = DaysBetweenDates(BIRTH_DATE,   DATE2)  
 
constant d3 = add(BIRTH_DATE, n, DAYS) -- should equal DATE2 
 
printf(1, "There are %d days between  %s and %s. \n\n",  
          {n, 
            dt:format(BIRTH_DATE, "%A, %B %d %Y"),  
            dt:format(DATE2, "%A, %B %d %Y") 
          }) 
           
printf(1, "%s plus %d days = %s. \n\n",  
          { 
           dt:format(BIRTH_DATE, "%A, %B %d %Y"), n,  
           dt:format(d3, "%A, %B %d %Y" ) 
          })           
 
-- output: 
 
-- There are 24268 days between  Wednesday, June 28 1950 and Tuesday, December 06 2016  
 
-- Wednesday, June 28 1950 plus 24268 days = Tuesday, December 06 2016  
new topic     » topic index » view message » categorize

2. Re: DaysBetweenDates - the easy way with Euphoria 4.0+

For comparison, this is what that looks like in Phix:
add/uncomment at the bottom of builtins\timedate.e

global function timedate_diff(timedate td1, timedate td2) 
    return timedate_to_seconds(td2)-timedate_to_seconds(td1) 
end function 

test code (same output):

include builtins/timedate.e 
 
set_timedate_formats({"DD/MM/YYYY","Dddd, Mmmm dd yyyy"},2) 
 
constant BIRTH_DATE = parse_date_string("28/6/1950") 
constant DATE2 = date() 
  
constant secs = timedate_diff(BIRTH_DATE, DATE2) 
constant days = floor(secs/(24*60*60)) 
  
constant d3 = adjust_timedate(BIRTH_DATE, secs) 
  
printf(1, "There are %d days between %s and %s. \n\n",   
          {days,  
            format_timedate(BIRTH_DATE),   
            format_timedate(DATE2)  
          })  
            
printf(1, "%s plus %d days = %s. \n\n",   
          {format_timedate(BIRTH_DATE), days,   
           format_timedate(d3 )  
          })            

I might add that the OE version should probably use datetime:format() to distinguish it from text:format().

new topic     » goto parent     » topic index » view message » categorize

3. Re: DaysBetweenDates - the easy way with Euphoria 4.0+

petelomax said...

I might add that the OE version should probably use datetime:format() to distinguish it from text:format().

Thanks, I amended the code in my initial post.

new topic     » goto parent     » topic index » view message » categorize

4. Re: DaysBetweenDates - the easy way with Euphoria 4.0+

Actually, the routine can be simplified further:

-- place this routine at the bottom of datetime.e: 
public function DaysBetweenDates(datetime d1, datetime d2) 
return diff(d1,d2)/DayLengthInSeconds 
end function 
-- test: 
include std/datetime.e as dt 
constant D1 = new(1950, 6, 28),  
         D2 = new(2016, 12, 7), 
         n = DaysBetweenDates(D1,D2),  
         D3 = add(D1, n, DAYS) -- should equal D2 
                
printf(1, "\n\tThere are %d days between  %s and %s \n\n",  
               {n,  
                   dt:format(D1, "%A, %B %d %Y"),  
                   dt:format(D2, "%A, %B %d %Y") 
                }) 
printf(1, "\n\t%s plus %d days = %s \n\n\t",  
               {   dt:format(D1,    "%A, %B %d %Y"), n,  
                   dt:format(now(), "%A, %B %d %Y") 
               })      
                     
-- output: 
 
-- There are 24269 days between  Wednesday, June 28 1950 and Wednesday, December 07 2016  
 
-- Wednesday, June 28 1950 plus 24269 days = Wednesday, December 07 2016  
 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu