3 min read

Date and Time - 2/6 - Number or a String

Moment, date and time definitions explained.
Date and Time - 2/6 - Number or a String
Photo by Towfiqu barbhuiya / Unsplash

So what is time? Is it a human construct only?

Will time become irrelevant once humans get to the next level of the evolution (some people say that is one of the messages of the Space Odyssey film)? Will we talk about GPS satellites gaining 38 microseconds a day confirming Einstein theories in practice?

Milan Junior: No philosophy please, can we just limit to planet Earth? Just practical examples.
Milan Senior: Sure, no problem (hehe, wait until we get to the time zones on planet Earth).

OK, let's explain some definitions.

Timestamp = Back-end time = Number

When we say “now” and clap our hands, that is a moment in time. We use numbers to represent it. One second later, we would get a different number. Let's call getTime method on a Date to see those numbers.

function test() {
  const dt = new Date(); // now
  console.log(`date=${dt}`);
  console.log(`timestamp=${dt.getTime()}`);
}

We would get something like:

date=Sun Mar 05 2023 19:35:17 GMT+0100 (Central European Standard Time)
timestamp=1678041317345

If we run the same code exactly 1 second later we would get:

date=Sun Mar 05 2023 19:35:18 GMT+0100 (Central European Standard Time)
timestamp=1678041318345

Method getTime() definition is “number of milliseconds since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC”. Indeed in two examples above we get 1 second difference, in timestamp number it's 1000.

You can try the Epoch Converter tool to check these conversions. You will notice it supports Unix timestamps in seconds, milliseconds, microseconds and nanoseconds.

Epoch Converter tool

Different systems need different precision. This tool guesses the precision based on the number you entered, the result output starts with the text, for example: "assuming that this timestamp is in milliseconds".

But it always works with integers, the whole numbers. JavaScript/Google Apps Script works in milliseconds, we get 1678041317345, we don't get seconds with the decimal point 1678041317.345, why? Because decimal point arithmetic has some rounding "surprises", but that's a topic for another post.

Timestamp is very handy, very practical. Sorting, order of the events, time difference... it's much easier with timestamp numbers. When you track the transactions on stock exchanges in different parts of the world, when you create the Calendar event, when you schedule a live stream... everything is saved as a timestamp. That's why I like to call it the back-end time. Later it is displayed to each user by translating it into the local date/time string, for example a meeting in your calendar.

Date and Time = Front-end time = String

Programmer: OK honey, we'll meet at 1676401200.
Girlfriend/Boyfriend: 😕 Programmers 🤬

Yeah, timestamp is practical to the computers, but we people like date and time. And we like it in a specific way. It would be simpler if we had a single time zone, for example UTC. Then everyone would be aligned.

Living in London: What time do you get up?
Living in Melbourne: Well, I'm an early bird, around 18:00.

We don't like that. We like 00:00 to be in the middle of the night and 12:00 to be in the middle of the day (we are not even consistent there, we'll get to that). We like to organize our dates in months and weeks.

So we have another system, local date and local time. We want all of our calendars, data, sheets, events to be in this format. Why? Because it's very handy, very practical.

The bottom line is: we have 2 systems, both of them practical, but converting between them is what gives us programmers a headache.


Questions, Corrections and Suggestions are appreciated: contact me.