Date and Time Manipulation

Marielle Lange mlange at widged.com
Sat Oct 28 19:59:04 EDT 2006


> While we are on the topic of dates, I've been thinking about  
> creating a "countdown" calendar, clocking the number of days  
> remaining between today's date and some future date (March 31,  
> 20010 specifically).  Any tips on how I can do this?  Do I have to  
> convert dates into seconds in order to subtract one from the  
> other?  Thanks.


Hi Marian,

In case this helps, I wrote a minimalist countdown utility for  
christmas last year:
http://revolution.widged.com/stacks/?category=widgets_ecards
Christmas Countdown

You will also find below various date manipulation functions. They  
were written rapidly and haven't been extensively tested. You are  
very welcome to improve them.
That library can be found at <http://codes.widged.com/?q=node/1>,  
along with other code and libraries.

Other websites with useful codes are listed at:
<http://codes.widged.com/?q=node/660>

There are some time related tutorials at So smarts Software, for  
instance (<http://www.sosmartsoftware.com/? 
r=revolution_didacticiels&l=en>)

Marielle


/* ____________________________________________________________
|
|   Date Processing Routines
|
|   @Author:         Marielle  Lange
|   @Company:     Widged.com (http://widged.com)
|   @Date:            10 Apr 2006
|   @Version:        0.1
|   @Changes since last version:  N/A
|   @License:       Creative Commons Attribution 2.5 License  http:// 
creativecommons.org/licenses/by/2.5/
|   @Dependency:    -
*/

on DiffBetweenDates
   put field "DateFrom" into tDateFrom
   put field "DateTo" into tDateTo
   convert tDateFrom from system date to dateItems
   convert tDateTo from system date to dateItems
   put NbYearsBetweenTwoDates(tDateFrom,tDateTo) into field "NbYears"
   put NbMonthsBetweenTwoDates(tDateFrom,tDateTo) into field "NbMonths"
   put NbWeeksBetweenTwoDates(tDateFrom,tDateTo) into field "NbWeeks"
   put NbDaysBetweenTwoDates(tDateFrom,tDateTo) into field "NbDays"
end DiffBetweenDates


----------------------------------------------------------------------
--  WeekdayFirstOfMonth (function)
--
--
--
function WeekdayFirstOfMonth pYear, pMonth
   --- First of Month
   put pMonth & "/1/" & pYear into tDate
   convert tDate to dateItems
   return item 7 of tDate
end WeekdayFirstOfMonth

----------------------------------------------------------------------
-- NbDaysInMonth (function)
--
--
--
function NbDaysInMonth pYear, pMonth
   put (pMonth & "/1/" & pYear) into tDate
   convert tDate from date to dateItems
   put 0 into item 3 of tDate
   add 1 to item 2 of tDate
   convert tDate from dateItems to dateItems
   return (item 3 of tDate)
end NbDaysInMonth


----------------------------------------------------------------------
-- NbDaysBetweenTwoDates (function)
--
-- pStart and pEnd are in dateItems format
--
function NbDaysBetweenTwoDates pStart, pEnd
   put NbMonthsBetweenTwoDates(pStart, pEnd) into tMonthsDiff
   put (item 1 of pStart) into tStartYear
   put (item 2 of pStart) into tStartMonth
   repeat with x = 1 to tMonthsDiff
     if x = 1 then
       put tStartMonth into tMonth
       put tStartYear into tYear
       put empty into tDaysPerMonth
       -- put empty into message
     end if
     if tMonth > 12 then
       put 1 into tMonth
       add 1 to tYear
     end if
     put NbDaysInMonth(tYear,tMonth) into item x of tDaysPerMonth
     add 1 to tMonth
   end repeat
   put sum(tDaysPerMonth) into tNbDays
   return (tNbDays - (item 3 of pStart) + (item 3 of pEnd))
end NbDaysBetweenTwoDates

----------------------------------------------------------------------
-- NbWeeksBetweenTwoDates (function)
--
-- pStart and pEnd are in dateItems format
--
function NbWeeksBetweenTwoDates pStart, pEnd
   put NbDaysBetweenTwoDates(pStart, pEnd) into tNbDays
   return trunc(tNbDays/7)
end NbWeeksBetweenTwoDates


----------------------------------------------------------------------
-- NbMonthsBetweenTwoDates (function)
--
-- pStart and pEnd are in dateItems format
--
function NbMonthsBetweenTwoDates pStart, pEnd
   ----- Nb of Years, Months, weeks -----
   put NbYearsBetweenTwoDates(pStart, pEnd) into tNbYears
   put ((item 2 of pEnd) - (item 2 of pStart))  into tNbMonthsDiff
   ----- Handle cases were from 08/2005 to 06/2006 -----
   if tNbMonths < 0 then
     add 12 to tNbMonthsDiff
     add -1 to tNbYears
   end if
   return tNbMonthsDiff+ (12 * tNbYears) e
end NbMonthsBetweenTwoDates

----------------------------------------------------------------------
-- NbYearsBetweenTwoDates (function)
--
-- pStart and pEnd are in dateItems format
-- Return an integer, with the number of years
function NbYearsBetweenTwoDates pStart, pEnd
   return ((item 1 of pEnd) - (item 1 of pStart))
end NbYearsBetweenTwoDates


----------------------------------------------------------------------
-- MonthsAsArray (function)
--
-- Returns an Array with the Month Names, as MonthsA
--
function MonthsAsArray
   ----- Create an array with the Months Names -----
   put empty into MonthsA
   repeat with x = 1 to 12
     put x & "/1/00" into tDate
     convert tDate to long date
     put word 2 of tDate into MonthsA[x]
   end repeat
   return MonthsA
end MonthsAsArray

----------------------------------------------------------------------
--  weekNumber
--
--
--
function weekNumber pDateItems
   -- Needs the 7, because any day before may below to a week in the  
previous year
   put pDateItems into tJanuaryFirst
   put 1 into item 2 of tJanuaryFirst
   put 1 into item 3 of tJanuaryFirst
   convert tJanuaryFirst from dateitems to dateitems
   put 0 into tAdd; if item -1 of tJanuaryFirst <> 1 then put -1 into  
tAdd
   return (NbWeeksBetweenTwoDates(tJanuaryFirst,pDateItems) + tAdd)
end weekNumber

----------------------------------------------------------------------
-- StartDateIsBeforeEndOne
--
-- Returns true or false, according to situation
--
function StartDateIsBeforeEndOne pStart, pEnd
   put true into tTest
   -----  Checking that the Start date is earlier than the End one -----
   If item 1 of pStart > item 1 of pEnd then
     answer "problem here, the ending year is earlier than the  
starting one"
     put false into tTest
   else if (item 1 of pStart) = (item 1 of pEnd) and (item 2 of  
pStart) > (item 2 of pEnd) then
     answer "problem here, the ending month is earlier than the  
starting one (in the same year)"
     put pStart & cr & pEnd
     put false into tTest
   else if (item 2 of pStart) = (item 2 of pEnd) and (item 3 of  
pStart) > (item 3 of pEnd) then
     answer "problem here, the ending day is earlier than the  
starting one (in the same month)"
     put false into tTest
   end if
   return tTest
end StartDateIsBeforeEndOne






More information about the use-livecode mailing list