Calculate Seconds, Hours, and Years
The following pushButton code snippet uses a similar technique to the one just demonstrated to display the number of seconds, hours, days, and years since your birth.
method pushButton(var eventInfo Event)
var
dtBirth Datetime
n Number
endVar
;Set your birthdate and time
dtBirth = DateTime("07:32:00 AM, 01/08/65")
dtBirth.view("Enter your birthday and time")
;show years, days, hours, minutes, and seconds old
n = number(year(today()) - year(dtBirth))
n.view("Number of years old")
n = number(today() - dtBirth)/1000/60/60/24
n.view("Number of days old")
n = number(today() - dtBirth)/1000/60/60
n.view("Number of hours old")
n = number(today() - dtBirth)/1000/60
n.view("Number of minutes old")
n = number(today() - dtBirth)/1000
n.view("Number of seconds old")
endmethod
Time Variables
A Time variable is stored in the format HH:MM:SS AM/PM. You could use any of the following as separators: blank, tab, space, comma, hyphen, slash, period, colon, or semicolon. If you want, type the following code into the ushbutton event of a button. (All the following strings are legal time strings in ObjectPAL.)
1: ;DATATYPE :: btnTimeFormats :: pushbutton
2: method pushbutton(var eventInfo Event)
3: var t Time endVar
4: t = time("10:05:32 AM")
5: t.view()
6:
7: t = time("10;05;32 AM")
8: t.view()
9:
10: t = time("10 05 32 AM")
11: t.view()
12:
13: t = time("10,05,32 AM")
14: t.view()
15:
16: t = time("10/05/32 AM")
17: t.view()
18: endMethod
Note that although you can type time value strings in any of the legal formats of ObjectPAL, the time in the displayed view box is the same because it is guided by the operating system. It's displayed to the user in the format specified in Control Panel. The values the user inputs must be in accordance with his or her Control Panel settings. Internally, ObjectPAL stores the time all the way down to the millisecond.
Return Current Time
Call Time() with no parameters to return the current time. For example, type the following code into the ushbutton event of a button:
1: ;Button :: pushbutton
2: method pushbutton(var eventInfo Event)
3: msgInfo("Current time", time())
4: endMethod
The user's Control Panel settings determine whether the 12-hour time format (HH:MM:SS AM/PM) or the 24-hour (military) time format is used. Sometimes you want to convert 12-hour time to 24-hour time, however. The following routine casts a string as Time and then displays it in 24-hour format. You can type the code into the ushbutton event of a button.
Demo Files: WORLD-T.FSL
1: ;Button :: pushbutton
2: method pushbutton(var eventInfo Event)
3: var
4: s String
5: t Time
6: endVar
7:
8: formatSetTimeDefault("hh:mm:ss am")
9: s = "2:20:00 PM"
10: t = time(s)
11: formatAdd("24", "to(%h:%m:%s)na()np()")
12: formatSetTimeDefault("24")
13: t.view()
14: endMethod
Subtract Two Times
Suppose that you need to get the number of seconds between two times. Type the following into the ushbutton event of a button:
1: ;Button :: pushbutton
2: method pushbutton(var eventInfo Event)
3: var
4: t1, t2 Time
5: endVar
6:
7: t1 = time("1:35:30 PM")
8: t2 = time("2:10:15 PM")
9: view(Number(t2 - t1) / 1000) ;For seconds.
10: view(Number(t2 - t1) / 1000 / 60);For minutes.
11: view(Number(t2 - t1) / 1000 / 60 / 60) ;For hours.
12: endMethod
DateTime Data type
DateTime is a special data type that stores both date and time values in the same variable. DateTime stores data in the form of hour-minute-second-millisecond year-month-day. The DateTime() method returns the current date and time. For example, to return the current date and time in a message information box, type line 3 into the ushbutton event of a button.
1: ;Button :: pushbutton
2: method pushbutton(var eventInfo Event)
3: msgInfo("Current Date & time", DateTime())
4: endMethod
As an interesting experiment, you can cast values with the procedure DateTime() and display the results. This experimenation helps you to understand the numbers behind the DateTime data type. For example, type line 3 into the pushButton event of a button:
1: ;Button :: pushbutton
2: method pushbutton(var eventInfo Event)
3: msgInfo("Beginning Date/Time", DateTime(0))
4: endMethod
Line 3 displays 12:00:00 AM, 00/00/0000 in a dialog box. This shows you that going back all the way to year 0 is legal in ObjectPAL. In fact, you can even use B.C. dates and times (negative numbers).
DateTimes are numbers!
If you store data in an alphanumeric field type and later need to do a calculation on the value, you must cast the string in the field as a number. To cast a string as a number, use Number and DateTime() together. For example, type lines 3-8 into the ushbutton event of a button:
1: ;Button :: pushbutton
2: method pushbutton(var eventInfo Event)
3: var
4: nBirth Number
5: endVar
6:
7: nBirth = Number(DateTime("07:30:00 PM, January 20, 1967"))
8: msgInfo("Lisa's Birthday", nBirth)
9: endMethod
The preceding line 8 displays the number 62042700600000 in a dialog box. This isn't very useful, but you can use this code in a calculation with values (including another DateTime converted to a Number).