Time ago in words? Calcing dateTimeDiff that returns DHMAgo?
Posted: 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
i.e. 2 Days, 3 hours, 4 mins ago
Questions and answers about the LiveCode platform.
https://www.forums.livecode.com/
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
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
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
Code: Select all
put tEndDate - tStartDate into tAgeInSeconds
BarrySumpter wrote:Why am i thinking that if the tAgeInSeconds is - negative,
that the ConvertDateTimeToAgeInDHMAgo routine could be used as a count down?