Storing a modification timestamp

This lesson demonstrates how to store the modification date of a stack. In this example only fields are used.

The closeField message

The closeField message is sent to a field whenever it loses focus after a change to the field's content. When the closeField message is sent to the field, it triggers this handler.

The closeField handler

In this example, the closeField handler should be placed in the stack script. This lets every field on every card update the modification date when the field is changed, without needing to put a copy of the handler in each field's script.

The handler stores the current date and time in a custom property called cLastModDate. Since the handler specifies that the cLastModDate custom property will be applied to the current card, this example sets, for each card, the date and time any of its fields was last changed.

The date and time are stored in the form of the seconds function, the number of seconds since the start of the eon. We store the date and time this way, rather than as a text date and time, for two reasons: because this format doesn't vary depending on the user's system settings, and because it's easy to do arithmetic on (if we want to use a handler to find out whether one date is before or after another).

The closeField handler code

on closeField 
	## place on stack script
	set the cLastModDate of this card to the seconds
	pass closeField
end closeField

Displaying a modification timestamp to the user

We may want to display the last-modification timestamp to the user. For example, we might put the date and time into a field whenever a card opens. For this purpose, we'll want to convert the seconds, which is simply a number, into something a little more user-friendly. The convert command changes the format of a date:

on openCard 
	local tDate
	## when the user goes to a card
	## get the custom property
	put the cLastModDate of the target into tDate
	## switch to a better format:
	convert tDate from seconds to date and time
	## display the converted date and time in a field:
	put tDate into field "Last Mod Date"
	pass openCard
end openCard


Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.