The Time Odyssey

Navigating Date Challenges in the Programming World

Mykola-Bohdan Vynnytskyi
6 min readJul 8, 2023
Photo by Icons8 Team on Unsplash

Working as a developer, sooner or later, you always come across one problem that, once it appears, won’t let go for a long time.
This is the problem that adds a hundred bugs and the same number of tasks to the project. This problem is like a disease that is very difficult to cure.

Think from the title of the article and the text above it is already clear what I want to talk about today — working with dates.
In this article, you will find the most common issues that can occur and how to avoid or resolve them.

Timestamp format

One of the initial challenges in a project arises when deciding on the format for receiving and transmitting dates. This may seem straightforward when working on a local project, where the time format is clear to all project participants, including clients.

For instance, there exists a standardized format called ISO 8601, designed to harmonize time representations.
It follows the pattern of YYYY-MM-DD (e.g., 2023–07–05).

At first glance, it may appear simple and universally recognized. Why continue reading this article?

The plot thickens when your project expands and operates across various regions worldwide. A user encounters the date as 2023–07–05.
Is it interpreted as July 5th, 2023, or could it be May 7th, 2023?

Let’s help our quirky clients by creating a date format that suits their preferences — how about Month D, Yr?
For example, July 05, 2023.

Problem solved, right? Not quite!

Although the client may now understand the date, we encounter a new challenge — we need to redesign our system to accommodate this new date format and implement parameter validation.

Congratulations on the upcoming tasks for the next sprint! 🎉

Now, let’s get serious and address this issue.
First and foremost, it’s essential to recognize that different countries may have one or even several different date formats. Trying to manually cover all of them is nearly impossible.
However, there’s good news: the modern world offers ready-made solutions for working with dates. I’m confident that your favorite programming language has one or even several libraries dedicated to this purpose.

In essence, the first step is to select existing solutions for working with date and time localization. These libraries can handle various date formats, making our lives much easier.

Secondly, computers are indifferent to how people write dates. They ultimately convert everything into numbers. To simplify matters, we can utilize the Unix format — representing the number of seconds since the beginning of the era (e.g., 1688504400). This format serves as a universal representation of date and time.

To summarize, we can allow users to display time in their preferred format. However, when passing this date to the backend, we can convert it to Unix format for processing and storage. By doing so, we eliminate the need for multiple validations of different date formats, as we unify them into a single format

Time zones

Now that we have resolved the date format challenges in our ever-expanding application, we may believe that everything is functioning as it should. But is it truly the case?

Considering our global user base, who reside in different time zones, we encounter a new consideration. For instance, when it’s 19:42 in Ukraine, it’s 09:42 in California. The concept of time becomes more intricate as we navigate across various regions and time zones.

World Time Zones Map

Why is this a problem, you may ask? Well, initially, one solution may be to restrict users to only view information created by themselves or others in the same time zone. However, implementing such strict restrictions not only limits user experience but also fails to account for scenarios where individuals travel across different time zones. What do we do in those cases?

One potential solution is to format the time zone whenever necessary. This approach may seem like a simple fix, but the underlying challenge remains. Formatting time zones on the fly introduces additional complexity and may lead to inconsistencies or errors in displaying accurate time information.

Indeed, if customer information is stored in different time zones, filtering data in the database to obtain specific subsets of information can lead to complications and potentially yield incorrect results. The variations in time zones can introduce discrepancies that hinder the accuracy of our queries and subsequent data retrieval.

Simply put, relying solely on the database’s native filtering capabilities may not suffice when dealing with time-zone-specific data.

To overcome this problem, we can simplify the data by converting each user’s time zone to Coordinated Universal Time (UTC) when stored in our system. By using UTC as the standard time zone, we ensure that all data is uniform and consistent, making it easy to filter and get accurate information.

By converting user-supplied time zones to UTC before saving them to the database, we create a common reference point for all data. This eliminates the complications associated with different time zones and provides robust data management and analysis.

This solves the problem before it occurs, to resolve the chaos that is already raging on the project, you need to align your data very carefully and meticulously.

Furthermore, even if we store the data in UTC format, we must remain cautious when retrieving results from the database or endpoints.
In many cases, the returned results will be adjusted to the local time zone (from UTC) or the time zone of the server running the web application. While it may not be possible to completely overcome this behavior, it is crucial to be aware of it and account for any potential discrepancies.

The following points are more about what we have to remember and understand than about what we have to handle manually.

Let’s move on)

Daylight Saving Time

Another factor to consider alongside different date formats and time zones is Daylight Saving Time (DST), which adds an additional layer of complexity.

Dealing with DST can indeed be confusing, especially when trying to obtain results that fall during the transition period before or after DST. This can result in partial results with different time offsets, such as +3 in one time zone and +2 in another.

Fortunately, many modern databases include built-in capabilities to handle DST-related issues. By unifying the dates, as discussed in the previous points, the database can effectively handle DST adjustments automatically.

It’s important to be aware that DST exists and to remain calm when encountering such results. Understanding the concept of DST and its impact on data retrieval will help alleviate any potential concerns or confusion.

By acknowledging the existence of DST and relying on the database’s built-in features, we can navigate through DST-related challenges with confidence.

Leap years

Amidst the intricate world of date handling, we encounter the fascinating phenomenon of leap years.
These special occasions add an extra day, February 29th, to our calendars, compensating for the Earth’s orbital journey around the sun. But this cosmic adjustment brings its own peculiar challenges when it comes to working with dates.

Leap years follow a set of rules: they occur every four years, except for years divisible by 100 but not divisible by 400. These exceptions ensure the precision and accuracy of our calendar system over extended periods.

Navigating the realm of leap years is essential in maintaining precise date calculations. Neglecting their existence can lead to discrepancies and unexpected results in time-based operations like duration calculations or date comparisons.

Thankfully, programming languages and date libraries come to the rescue, providing dedicated functions and methods to handle leap year calculations with ease. By harnessing these tools, we can ensure our date operations remain accurate and reliable, accounting for the quirks of leap years.

Conclusion

Time, with all its mysteries and challenges, remains beyond our control. However, by gaining awareness of the problems that arise when working with time, we can navigate the complexities more effectively.

Throughout this article, we have explored the most common issues encountered when handling time. The key takeaway is that these challenges are inevitable, but it’s always better to confront them sooner rather than later.

Have you encountered similar issues?
Share your stories about working with dates in the comments section!)

Thanks for reading this article.
See you!

--

--

Mykola-Bohdan Vynnytskyi

Data Engineer by day. YouTuber, author, and creator of courses by night. Passionate about Big Data and self-development.