How to use date and time in Rev

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.

Rev 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

Getting the date

In Rev 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

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.

Rev has a number of different formats for dates, you can specify which form you want the date returned in.

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

Converting the date

You can use the convert command to convert a date between one format and another

put "12/31/2010" into tDate

convert tDate to long date

put tDate into field "Christmas Day"

Formatting the date yourself

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

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

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

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

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

Coverting the time

Coverting the time

Again this is the same as coverting 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

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"

4 Comments

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

Add your comment

E-Mail me when someone replies to this comment