What Is a Timestamp?
A timestamp is a value that generally includes year, month, day, hour, minute and second and time zone. Timestamps can show up in quite a few formats. But, in each case, they are recording the specific time that something happened in a digital device. The key is that what you see is actually, just a formatted value, not the original value itself.
In the photo on the right, the timestamp values have been pulled apart and put into different areas on the display. The format is military time, hence in the main display, you see 20 for the hour and 09 for the minute. But in the center of the right column, you see month and date. Year, second and time zone are not even used.
What Does a Timestamp Look Like?
- 2018‑1‑1 01:02:03
- 2018‑01‑01 1:2:3
- And, many more
But, these are timestamps that are all dressed up and ready to go out ( formatted to a defined specification). There’s more underneath all the finery.
Why Are There so Many Characters in a Timestamp?
The truth is, that in the computer, a timestamp is just a long integer value. And, and not all timestamps even have the same number of digits, they could be: 10 digits, 13 digits, 16 digits, 19 digits. So, if you are working with timestamps, it’s good to have a little background information about the origin and format of the timestamp to use in your decision making.
You may see a T or a Z in a timestamp. The T separates the date from the hours. The Z stands for Zulu or UTC time zone at 0° longitude. It’s the current version of “Greenwich Mean Time”. Times in other places are calculated with an “offset”. The offset for Mountain Time (MT) is -7 hours. The -7 is a good clue that, not only can you pull a timestamp apart, you can do math on a timestamp or a part of a timestamp.
Where Does a Timestamp Come from?
A timestamp is the record of a specific internal time in some digital device. There is no implied accuracy in that timestamp because the device’s internal clock can be wrong, whether deliberate or not. The reason there is a Universal Coordinated Time (UTC) is to allow there to be a “real” timestamp that is agreed on worldwide. The accuracy of a timestamp may or may not be part of your job. Your job may be just to retrieve and use a timestamp stored somewhere. Or, your job may be to add code to retrieve a timestamp from a machine and store it.
“A special kind of system memory called CMOS stores the date and time settings and a computer chip called the Real-Time clock (RTC) then keeps track of time. When the PC is powered off, a battery on the motherboard keeps the CMOS memory and the RTC chip activated so it does not lose time.”
~ This quote is from a very interesting PDF – see link below.
How to Pull the Date out of a Timestamp
A very common task for a developer is to show the only day, month and year parsed from a timestamp on a website. Viewers don’t want to see all the extra stuff they aren’t used to. So, you have to pull out only the bits you want in the format you want. Like other data manipulation, it’s I have X, but I want Y.
The tools to parse and use the data in a timestamp are already exist for your language in the form of functions in libraries. All you have to do is look up the functions you want for your language. Once you are familiar with what a timestamp looks like, the functions make sense.
Example from PHP
- The PHP function date() is part of the core PHP, so there is no need to install other stuff (libraries, etc) to use it.
- Date() has two parameters: $format and $timestamp. date($format, $timestamp). You actually don’t need to include the timestamp, unless you are getting the timestamp from some other source than the computer’s internal function. For example, if the timestamp is coming from an outside data source, such as a database query, you will have to put that timestamp into a variable that is then passed into the function.
- The fun part is $format. The list of options is very long, but it allows you to get the date format you want.
- An example: $format = “Y/m/d H:i:s”; The hard part is getting the correct codes and punctuation in there.
- The characters in the example can be somewhat less understandable than the timestamp itself. So, we often use a table to figure out what the codes are for the pieces we want. There is a link in References below that shows all the codes for pulling bits and pieces out of a timestamp. There is a link in the References below.
Example from React
- Additionally, you add (import) into your project code base. One library option is moment.js