DEV Community

Cover image for Find out what a "Timestamp" is for
Coding Bugs
Coding Bugs

Posted on

Find out what a "Timestamp" is for

My nickname in the networks is Coding Bugs, and I have been working as a software developer and architect for more than 15 years. In my 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:

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

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.

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 account.

Credits

The header image comes from Unsplash and is courtesy of Jonathan Cosens Photography.

Top comments (0)