Find out what a "Timestamp" is for
Tags: programming, beginners
Published: 2022-01-10 09:20:03

> _My nickname in the networks is [__Coding Bugs__][Dashboard], and I have been working as a software developer and architect for more than 15 years. In my [dashboard][Dashboard], you will find articles about programming languages, programming techniques, and other concepts that will help you improve._

The _"timestamp"_ is a way of setting the current date and time to a set of values. The simplest example corresponds to each record in a database saving the date when of creation. So, checking when they have been added, ordering them by the creation time, or grouping them by months, weeks, or days can help their tracking.

The timestamp is a way of registering additional temporary information to any element we are handling. For example, photography cameras allow you to add the date and time when you press the shutter button and take the photo, Post offices also mark letters and packages upon receipt and delivery.

In our digital world, many and varied activities include timestamping. When you create a file on your computer, the operating system adds the created date and time as a property of your file. And, in addition, if you or a computer process modify the file content, you can check the latest modification date value of the file in a different property. Both values ​​are timestamps because they record two events related to the document, its creation, and the latest modification.

The value of the timestamp must be __consistent__ in its format because of comparisons between its values. In some cases, the date will be the only thing worth saving while, in more specific scenarios, we might need the date and time and, even, milliseconds. But this depends on the design of our application and the stated requirements.

Timestamps are used in log systems to save helpful information to trace applications, data, or processes. Databases also use them to set the creation or modification of a record, and even the date on which it is no longer helpful or logically deleted. Source code management systems like Git also used timestamps: each _"commit"_ records its date and time. Finally, _"blockchain"_ technology is used to establish the generation moment of each block.

__Incorporate a timestamp into your application__

Now let's go with an example and apply timestamps in an imaginary application. Imagine that we need to record the date and time when we receive a request to any method of our API. The data that we will have to save will be the following:

```JavaScript
let log_data = {
	endpoint: '', // API method URL
	verb: '', // ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', ...]
	created: new Date().toISOString(), // timestamp
	// more helpful information
};
```

In the following code, we create an object to store several values helpful to track the API methods invocations. The _"created"_ property keeps the date and time, and its value is the current date after creating the object. We can follow the timeline and see the methods that have been invoked over time dumping this information to a human-readable format in a local file.

For more information about the format of timestamps in the digital world, I recommend reading how to represent dates and times set in [ISO 8601][ISO].

__Summarizing__

Timestamps allow you to set a date and time based on the events of data: creation, modification, deletion, etc. They are a simple way to follow up on the data and to be able to analyze their history.

__Feedback__

If you liked this article, do not forget to click on the heart or the unicorn and share it with your friends and colleagues. Leave me your comments at the bottom about what you liked or how to improve and if you want me to write about other topics send me a direct message to my [Twitter][Twitter] account.

__Credits__

The header image comes from [Unsplash][Unsplash] and is courtesy of  [Jonathan Cosens Photography](https://unsplash.com/@jcosens).



[Wikipedia]: https://en.wikipedia.org/wiki/Timestamp "Marca temporal"
[ISO]: https://www.iso.org/iso-8601-date-and-time-format.html "ISO 8601"
[Unsplash]: https://unsplash.com/ "Unsplash"
[Twitter]: https://twitter.com/BugsCoding
[Dashboard]: https://dev.to/codbugs "Dashboard"