LessonsIntermediate: Stacks, objects and dataLesson 35

Dates and times

Read the clock, show a date the way you want, work out what day a date was, and count the days between two dates.

  • Intermediate
  • Lesson 35 of 83
  • About 10 minutes
  • 9 examples

Reading the clock

xTalk has functions for now: the date, the time, and the same in long and abbreviated forms. the seconds counts seconds since the start of 1970, and the milliseconds thousandths of a second, which are handy for timing things.

ExampleRun it in the playground
put the date
put the long date
put the time
put the seconds
Output (yours will differ: it changes from run to run)
11/5/26
Thursday, November 5, 2026
9:55 PM
1793915714

the date uses the American order, month first, so that scripts always read and write it the same way, wherever they run. the system date gives it in the order of the user's own settings, for showing to people.

Changing a date's form: convert

convert turns a date or time from any form xTalk understands into another: short date, long date, abbreviated date, seconds, internet date or dateItems. It changes the variable in place.

ExampleRun it in the playground
put "7/20/1969" into tDate
convert tDate to long date
put tDate
convert tDate to abbreviated date
put tDate
put "1/15/2024" is a date
put "2/30/2024" is a date
Output
Sunday, July 20, 1969
Sun, Jul 20, 1969
true
false

Taking a date apart: dateItems

In dateItems form a date is seven items: year, month, day, hour, minute, second, and the day of the week (1 is Sunday). It is the form to use for arithmetic:

ExampleRun it in the playground
put "3/1/2024 2:30 PM" into tWhen
convert tWhen to dateItems
put tWhen
put "Year" && item 1 of tWhen & ", hour" && item 4 of tWhen
put item 7 of tWhen is 6
Output
2024,3,1,14,30,0,6
Year 2024, hour 14
true

1 March 2024 was day 6 of its week, a Friday.

Adding days and months

To move a date, add to an item of its dateItems and convert it back. You need not worry about the ends of months or years: a day 32 or a month 13 rolls over into the next one.

ExampleRun it in the playground
put "12/31/2023" into tDate
convert tDate to dateItems
add 1 to item 3 of tDate
convert tDate to long date
put tDate
put "3/1/2024" into tDue
convert tDue to dateItems
add 30 to item 3 of tDue
convert tDue to short date
put tDue
Output
Monday, January 1, 2024
3/31/24
ExampleRun it in the playground
put "1/31/2024" into tDate
convert tDate to dateItems
add 1 to item 2 of tDate
convert tDate to short date
put tDate
Output
3/2/24

Days between two dates

Convert both dates to seconds, subtract, and divide by the seconds in a day:

ExampleRun it in the playground
function daysBetween pFirst, pSecond
   convert pFirst to seconds
   convert pSecond to seconds
   return round((pSecond - pFirst) / (24 * 60 * 60))
end daysBetween

put daysBetween("1/1/2024", "3/1/2024")
put daysBetween("1/1/2023", "1/1/2024")
Output
60
365

The round is there for the days the clocks change, which are an hour longer or shorter than the rest.

Your own format

For a form of your own, such as the international 2024-01-05, take the date apart and put it together with format, which pads the numbers with zeros:

ExampleRun it in the playground
function isoDate pDate
   convert pDate to dateItems
   return format("%04d-%02d-%02d", item 1 of pDate, item 2 of pDate, item 3 of pDate)
end isoDate

put isoDate("1/5/2024")
put isoDate("12/25/2023")
Output
2024-01-05
2023-12-25

Timing your code

Subtract the milliseconds before from the milliseconds after to see how long something took. The lesson Making it fast uses it a lot.

ExampleRun it in the playground
put the milliseconds into tStart
repeat 100000 times
   add 1 to tCount
end repeat
put the milliseconds - tStart && "ms"
Output (yours will differ: it changes from run to run)
12 ms

Try it yourself

On what day of the week was the first Moon landing, 20 July 1969? Put the name of the day, worked out from item 7 of its dateItems and a list of the names of the days.

Show a solution
ExampleRun it in the playground
put "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" into tDays
put "7/20/1969" into tDate
convert tDate to dateItems
put item (item 7 of tDate) of tDays
Output
Sunday

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.