Time ago in words? Calcing dateTimeDiff that returns DHMAgo?

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Time ago in words? Calcing dateTimeDiff that returns DHMAgo?

Post by BarrySumpter » Thu Mar 22, 2012 9:18 am

anyone have a script for calculating dateTimeDiff that returns DHMAgo?
i.e. 2 Days, 3 hours, 4 mins ago
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by sturgis » Thu Mar 22, 2012 3:33 pm

EDIT: Not sure about handling daylight savings time changes so your mileage may vary.

Something like this should get you started.

put "3/23/11 8:23 AM" into tThen -- the start time
convert tThen to seconds -- I find it easier to work with seconds for this

--to use a date range, you can do the same as for above
--get the date and time in a var and convert to seconds
-- otherwise just "put the seconds into tNow
--put the short date && the short time into tNow
--convert tNow to seconds

-- just using the current seconds for the calc
put the seconds into tNow

-- find the difference
put tnow - tThen into tDiff

-- do the calcs using the number of seconds difference (tDiff)
put trunc(tDiff / (60 * 60 * 24)) into tDays
put trunc(tDiff / 60 / 60) mod 24 into tHours
put trunc(tDiff / 60) mod 60 into tMin
put tDiff mod 60 into tSec

-- displays the info of course
put merge("Days: [[tDays]] Hours: [[tHours]] Minutes: [[tMin]] Seconds: [[tSec]] ago." )

Also, Sarah Reichelt has some great stuff. There is a date & time stack you can get here http://www.troz.net/rev/index.irev?cate ... ary#stacks that shows how to do all kinds of time/date gymnastics. (lots of other great stuff on the site too)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by bn » Thu Mar 22, 2012 3:42 pm

Hi Sturgis,

I also was going to recommend Sarahs stacks. She is the top expert on day and time. But I did not find a handler to give the difference between two dates.

The way you recommend one falls foul of dayligth saving time. I tried that and the only solution I found was to convert to internet time and then to seconds.

Here is a little stack that should work across changes of daylight saving time.
timeDifferenceII.livecode.zip
(1.96 KiB) Downloaded 258 times
Kind regards

Bernd

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by sturgis » Thu Mar 22, 2012 3:46 pm

Cool, didn't occur to me to do the internet time convert thingy thx! Added your stack to my future use library stuff.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by BarrySumpter » Thu Mar 22, 2012 9:51 pm

Code extracted from bernds timeDifferenceII.livecode.zip

Code: Select all

on mouseUp
   
   put field 1 && field 2 into tStartDate    --03/25/12 and 01:00 am
   convert tStartDate to internet date
   
   put tStartDate into field 6
   convert tStartDate to seconds
   
   put field 3 && field 4 into tEndDate      --03/25/12 and 01:11 pm
   convert tEndDate to internet date
   
   put cr & tEndDate after field 6
   convert tEndDate to seconds
   
   put tEndDate - tStartDate  into tAllSeconds
   put 60*60*24 into tADayInSeconds
   put 60*60 into tAnHourInSeconds
   put 60 into tAMinuteInSeconds
   
   put tAllseconds div tADayInSeconds into tTheDays
   put tAllSeconds mod tADayInSeconds into tHoursLeft
   put tHoursLeft div tAnHourInSeconds into tTheHours
   put tHoursLeft mod tAnHourInSeconds into tMinutesLeft
   put tMinutesLeft div tAMinuteInSeconds into tTheMinutes
   
   --put tStartDate && tEndDate into field 5
   
   put "Days: " & tTheDays & cr & "Hours: " & tTheHours & cr & "Minutes: " & tTheMinutes into field 5

end mouseUp

Thanks gents.
Very cool, concise and easy to read.
I'll be using a combination of both scripts.
You've both been recommended to the LC scripters hall of fame!

I did have a look at Sara's stack but didn't find what I needed.
I recall wanting a date format of yyyy dd MM hh:mm
but wasn't there either.

I think instead of us recommending Sarah's stack
that we recommend that Sarah updates her stack.

Or perhaps start a thread dedicated to Sarah's stack
and improvements to it.

Thanks again!
Another 4 hour hump smoothed over.
Last edited by BarrySumpter on Fri Mar 23, 2012 2:23 am, edited 3 times in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by BarrySumpter » Thu Mar 22, 2012 11:30 pm

Can't tell you how nice this is to read.

on ConvertDateTimeToAgeInDHMAgo:

Code: Select all

on mouseUp
   -- ConvertDateTimeToAgeInDHMAgo "2012-03-26 08:50 am"
   put ConvertDateTimeToAgeInDHMAgo ("03/23/2012 11:50 am") into x
   Answer x
end mouseUp

function ConvertDateTimeToAgeInDHMAgo DateTimeStamp
   -- NOTE: DateTimeStamp must me in MM/dd/yyyy hh:mm am/pm format
   --           Look up convert in the LC Dictionary
   --           http://lessons.runrev.com/s/lessons/m/4071/l/12354-how-to-use-date-and-time-in-rev
   
   --           am/pm - if not am/pm then    set the twelveHourTime to false   -- then convert
   


   Put DateTimeStamp into tStartDate
   convert tStartDate to internet date
   convert tStartDate to seconds
   
   put the seconds into tEndDate
   
   put tEndDate - tStartDate  into tAgeInSeconds
   put 60*60*24 into tADayInSeconds
   put 60*60 into tAnHourInSeconds
   put 60 into tAMinuteInSeconds
   
   put tAgeInSeconds div tADayInSeconds into tTheDays
   put tAgeInSeconds mod tADayInSeconds into tHoursLeft
   put tHoursLeft div tAnHourInSeconds into tTheHours
   put tHoursLeft mod tAnHourInSeconds into tMinutesLeft
   put tMinutesLeft div tAMinuteInSeconds into tTheMinutes
   
   
   
   If tTheDays > 0 then
      if tTheDays > 1 then
         put "Days" into DaysWord
      else
         put "Day" into DaysWord
      end if
      Put  tTheDays && DaysWord into DaysPhrase
   else
      put empty into DaysPhrase
   end if
   
   
   if tTheHours > 0 then
      if tTheHours > 1 then
         put "Hours" into HoursWord
      else
         put "Hour" into HoursWord
      end if
      Put  tTheHours && HoursWord into HoursPhrase
   else
      put empty into HoursPhrase
   end if
   
   
   if tTheMinutes > 0 then
      if tTheMinutes > 1 then
         put "Mins" into MinsWord
      else
         put "Min" into MinsWord
      end if
      Put  tTheMinutes && MinsWord into MinsPhrase
   else
       if HoursPhrase is empty and MinsPhrase is empty then
         put "Less than a Minute" into MinsPhrase
      end if
   end if
   
   Return DaysPhrase && HoursPhrase && MinsPhrase && "Ago" 
   --Return tTheDays && DaysWord && tTheHours && HoursWord && tTheMinutes && MinsWord && "Ago" 
   
end ConvertDateTimeToAgeInDHMAgo





========
my notes:
Now that this nice n readable display is done,
I'm working backwards towards storing DateTimeStamp in Valentina.
The DataTimeStamp needs to be readable in vStudio as YYYY-MM-DD hh:mm:ss

Then retrieved from vDB and converted to mm/dd/yyyy hh:mm:ss for this routine. ????

The following routine is smart.
1) first, actually set the Date format for the DB to yyyymmdd
to tell the database ' this is how i will be submitting dates
2) retrieve the date-separator and the Time-seperator
( previously set - maybe should set both FIRST for current system??? how if on mobile)
3) uses retrieved date separator to format date field value
4) uses retrieved time separator to format time field value
5) then adds record

so in the future when retrieving data for LC Age routine,
first, actually set the Date format to Kmdy ??? i.e. make the db do all the work - nice
then retrieve records

so instead of me being nervous or timmid or afraid of the DB.
Probably coming from not know the db or not using the db.
"Oh please DB tell me what format you are in and i'll write code around your settings"
instead I take ownership and have the DB perform cartwheels for me
"Your my DB. (u b mi DByotch!) I'll tell you how I want you to work. The dates will be in this format. Now save my date like I told you to."
Make me feel more in control.
Must remember not to have double shots of coffee in the mornings.


NO NO NO - to add dates with valentina they have to be in mm/dd/yy format
-------------- even if I set the date format to YMD the add must stil be in mm/dd/yyyy
-------------- It may be that the setting the date format in valentina is for retreival only

Code: Select all

function AddRecords
   
  get VDatabase_DateFormat( mDBRef, "kYMD" )
  put field DateSepEdit into date_separator
  put field TimeSepEdit into time_separator
  
  repeat with i = 1 to 10
    get VTable_SetBlank( mTbl )
    get VField_Value( mFld1, "2005" & date_separator & "01" & date_separator & i )
    
    put Valentina_ErrString() into msg1
    put Valentina_ErrNumber() into num
    if num <> 0 then
      msgbox msg1
    end if     
    
    get VField_Value( mFld2, "13" & time_separator & "50" & time_separator & i )
    
    get VTable_AddRecord( mTbl )
  
  end repeat
   
end AddRecords

Last edited by BarrySumpter on Thu Mar 29, 2012 8:51 am, edited 4 times in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by BarrySumpter » Fri Mar 23, 2012 4:46 pm

Why am i thinking that if the tAgeInSeconds is - negative,
that the ConvertDateTimeToAgeInDHMAgo routine could be used as a count down?

SO instead of Ago it could be: 4 hours 3 mins To Go

Code: Select all

put tEndDate - tStartDate  into tAgeInSeconds
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by sturgis » Fri Mar 23, 2012 8:30 pm

Because its a logical progression!
BarrySumpter wrote:Why am i thinking that if the tAgeInSeconds is - negative,
that the ConvertDateTimeToAgeInDHMAgo routine could be used as a count down?

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Time ago in words? Calcing dateTimeDiff that returns DHM

Post by BarrySumpter » Sun Mar 25, 2012 3:02 am

Just need to log this somewhere.
I'm thinking when we set the kYMD setting its only for the format when reading records with dates.
And NOT the format when adding records with dates.

When working with the DateTime field (not just the Date field) in Valentina.

I could not get the add record to work on a DateTime field,
untill I used the very specific format for the date of mm/dd/yyyy
Even though the v4Rev sample shows you can use 2005-01-23 for a Date field
I can't with a datetime field. Well, I couldn't get it to work.
I don't know why

for me its not clear as to what each valentina function is doing with the Get LC directive.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”