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:
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"
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 "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"