How to use date and time in LiveCode
There are many situations where you will want to use the date or the time within your application. Maybe you had a database application and want to store the date whenever you add a record, maybe you want to create a to do list, maybe you just want to time how long something takes.
LiveCode has built in functions for getting the date and time, in a range of formats. This lesson will show you how to use them.
Getting the date
In LiveCode finding out what the date is couldn't be simpler, its a single line of English. Just type into the message box
put the date
Different date formats
But what if the date isn't in the format you are used to, for example that date above is in the form m/d/y but here in the UK the more common form is d/m/y.
LiveCode has a number of different formats for dates so you can specify which form you want the date returned in. This is the script of the "Get Dates" button in the screenshot:
on mouseUp
put the long date into field "long"
put the abbreviated date into field "abbreviated"
put the short date into field "short"
put the english date into field "english"
put the system date into field "system"
put the internet date into field "internet"
end mouseUp
Converting the date
You can use the convert command to convert a date between one format and another. Add another field named "Christmas Day" and add this to your mouseUp handler in the Get Dates button:
put "12/25/2017" into tDate
convert tDate to long date
put tDate into field "Christmas Day"
Formatting the date yourself
If none of the built in formats give you what you need then you can format the date yourself, the easiest way to do this is using the dateItems, this allows you to convert a date to a comma delimited list made up of
* the year
* the month number
* the day of the month
* the hour in 24-hour time
* the minute
* the second
* the numeric day of the week where Sunday is day 1, Monday is day 2, and so forth
You can then use these items to format your date to display however you want. For example, add a field named "formatted items" to your sample stack, and add this script to your mouseUp handler in the button:
put the date into tDate
convert tDate to dateItems
put "The current day is" && item 3 of tDate & return into tMyFormattedDate
put "The current month is" && item 2 of tDate & return after tMyFormattedDate
put "The current year is" && item 1 of tDate after tMyFormattedDate
put tMyFormattedDate into field "formatted date"
Getting the time
Getting the time is almost exactly the same as getting the date, again just a single line of English
put the time
And similarly you can specify the format you want for the time. Change your button script to read:
put the long time into field "long"
put the abbreviated time into field "abbreviated"
put the short time into field "short"
put the english time into field "english"
put the system time into field "system"
12 and 24 hour time
If you want to use 24hour time you need to set the twelveHourTime property to false
set the twelveHourTime to false
put the time
Converting the time
Again this is the same as converting the date, just using the time instead.
put the time into tTime
convert tTime to long time
put tTime into field "Converted Time"
Formatting the time yourself
As with the date you can use dateItems to create a custom time format
put the long time into tTime
convert tTime to dateItems
put tTime
put "The time is" && item 5 of tTime && "minutes and" && item 6 of tTime && "seconds past" && item 4 of tTime into tMyFormattedTime
put tMyFormattedTime into field "formatted time"
Al C
I'm not sure the english date function works properly--even in the example above. (Wouldn't the first of April, 2010 be 1/4/10?)
Hanson Schmidt-Cornelius
Hi Al C,
intuitively you may expect so, but "the english date" refers to the US english date, where the data is commonly represented as month-day-year.
Kind Regards,
Hanson
Yahsa
Hi,
How could I get livecode to calculate elapsed days or time from a given date or time?
Thank you
Yasha
Elanor Buchanan
Hi Yasha
You can calculate the number of elapsed days between to dates by converting the dates into seconds, working out the difference and converting the number of seconds into a number of days. Check the convert command and seconds keyword in the Dictionary.
Assuming you have a field called "date", where you enter the date in the past, the code to calculate the number of days between today and the date would be
on mouseUp
put the date into tNow
convert tNow to seconds
put field "date" into tThen
convert tThen to seconds
put tNow-tThen into tDifferenceInSeconds
## Convert the number of seconds into a number of days
answer tDifferenceInSeconds / (60 * 60 * 24) && "days"
end mouseUp
You can do something similar with the time.
I hope that helps.
Kind regards
Elanor
trevix
do this:
put "3/23/17" into tdate
convert tdate to seconds
put "3/27/17" into tdate2
convert tdate2 to seconds
put (tdate2 - tdate)/86400 into tDays
put tDays
the result will be 3.958333 (??!! would be nice to know why)
so it must be
put round((tdate2 - tdate)/86400) into tDays
Roberto Trevisan
This does not work if there is a daylight saving time between the two dates.
For example, in italy the daylight saving time is the 26/3/17. At two o'clock in the morning the clock will be switched to 3 o'clock. This script:
put "3/26/17" into tdate
put tdate into tResult
convert tdate to seconds
put "3/27/17" into tdate2
convert tdate2 to seconds
put (tdate2 - tdate) into tDifference
put tDifference/86400 into tDays
put tDays
will return tDays = 0.958333 (that is not 1 as it should)
So...how is it possible to always have a day difference between two dates?
Regards
Trevix
Elanor Buchanan
Hi Roberto
If you calculate the difference using midday rather than midnight the number of days comes out correct.
Credit for the solution goes to Sturgis on the forum
http://forums.livecode.com/viewtopic.php?f=11&t=23873
Kind regards
Elanor
Elanor Buchanan
Sorry, the example code is
on mouseUp
local tDate, tResult, tDate2, tDifference, tDays
put "3/26/17" && "12:00 PM" into tdate
put tdate into tResult
convert tdate to seconds
put "3/27/17"&& "12:00 PM" into tdate2
convert tdate2 to seconds
put (tdate2 - tdate) into tDifference
put tDifference/86400 into tDays
put tDays
end mouseUp
Trevix
While trying to parse user "time" input, I found this:
put "010:44" into tTime
convert tTime to system time
--tTime = 10:44 correct
but:
put "0100:44" into tTime
convert tTime to system time
--tTime = 0100:44 wrong.
How come?
I also tried format("%02s",item 1 of OraPost) & ":" & format("%02s",item 2 of OraPost) but if there are extra char they are not reduced to two digit
Would be nice a complete example of time parsing that takes in account:
- extra digits, wrong digits
- "," ":" "." separators
- 12-24 hours time
Elanor Buchanan
Hi Trevix
The reason that 0100:44 is not converted is that it is an invalid time, if you check the result after the convert command it will say "invalid data" and the original time will be unchanged.
When using the format function if a charLength is specified, if the string is shorter than the charLength, enough leading characters are added to make it charLength characters long. If the length of the string is equal to or greater than the charLength, it is unchanged.
We will look into writing a more detailed lesson on time parsing but if there is something specific you are trying to achieve right now please let me know and I will try to help.
Kind regards
Elanor
Marcha
I thought this might also be useful to somebody else: I needed a solution to return the UTC time with additional information and used powershell on windows10:
I store this in a function and call it whenever needed.
put "powershell.exe $a = Get-Date; $a.ToUniversalTime().ToString('dd,MM,yyyy,HH,mm,ss,MMMM,dddd,zzz') + ',' + $a.IsDaylightSavingTime();" into tPowerShell
answer "UTC time: " && shell(tPowerShell)
Marcha
Forgot: Use the line
set the hideConsoleWindows to true
before the above lines of code
Michael Anderson
So, still trying to work out how to use the old unix timestamps our database has. I suppose I could covert everything and store it in a different format, except I have a lot of code rewriting to do, and as fast as live code is, that would take me many months. Our database stored everything as a unit time stamp, seconds from midnight(utc) Jan 1st, 1970. Now I need to be able to access the information with live code, both reading and writing data to the database. Any ideas?
Elanor Buchanan
Hi Michael
LiveCode understands seconds as a time/date format so all you would need to do is to convert the seconds to the format you need within the app. For example
put the seconds into tSeconds
convert tSeconds to long time
put tSeconds
Returns 10:13:20 AM, where the seconds was originally 1548065600. You can do the inverse if you want to write seconds back to the database.
Have a look at the entries for seconds and convert in the Dictionary for a bit more information.
I hope that helps.
Elanor
Michael
I guess what's confusing me is how to set are arbitrary time and date at the same time... I can set the time or date, how do I set both for a day back in December for example?
Elanor Buchanan
I tend to use the dateItems to work with date and time at the same time. This is described in the "Formatting the date yourself" section of the lesson. So you could take the seconds from the database, convert to dateItems to get the year,month,day,hour,minute,second,day of the week and then format that for display however you need. You can do the inverse to update the database.
put 1548065600 into tSeconds
convert tSeconds to dateItems
put tSeconds
gives
2019,1,21,10,13,20,2
I hope that helps.
Elanor
tim
Hello, is there an easy way for this format or do i have to use if statements ?
30.01.2021 ;19:14:52
Thank you
Elanor Buchanan
Hi Tim
I would recommend converting to dateItems. This is the easiest way to format a date and time into your preferred format. You can use the format function to add leading 0s where needed.
Something like the function below should work.
I hope that helps.
Elanor
function getFormattedDate pDate
local tFormattedDate
convert pDate to dateItems
put format("%02s",item 2 of pDate) & "." & format("%02s",item 2 of pDate) & "." & item 1 of pDate into tFormattedDate
put ";" & format("%02s",item 4 of pDate) & ":" & format("%02s",item 5 of pDate) & ":" & format("%02s",item 6 of pDate) after tFormattedDate
return tFormattedDate
end getFormattedDate
Daniel
Dear Elanor
Your getFormattedDate pDate is almost perfect. Only the item 2 should be replaced with item 3. The other codes shows how nice LiveCode can be.
Daniel