Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Feature Engineering From Dates and Times
AI & Machine Learning

Feature Engineering From Dates and Times

A timestamp is one of the richest fields you hold, yet most models get it as a bare number. What to extract, and the traps around holidays and time zones.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

Raw timestamps are nearly useless to a model. Day of week, position in month, proximity to holidays and elapsed time since a prior event carry most of the signal, and cyclical encoding stops the model treating December and January as far apart.

The most wasted column in business data

Almost every business record has a timestamp, and it is frequently handed to a model as a raw number. In that form it encodes a trend and little else.

Business activity is driven by calendar structure: weekly rhythms, month-end, pay dates, holidays, school terms. All of that is recoverable from a timestamp, and none of it is visible in the raw value.

What is usually worth extracting

  • Day of week - almost always significant in any business with customers
  • Position in month - first working day, month-end, and the gap between them
  • Week of year - seasonality, though beware year boundaries
  • Hour of day - for anything with an intraday pattern
  • Working day or not - including the local holiday calendar
  • Days until or since a holiday - often stronger than a simple flag
  • Elapsed time since a previous event - days since last order is frequently the single most predictive feature available

That last one deserves emphasis. In customer models, recency does a great deal of work and it is derived rather than stored.

Cyclical encoding

Day 31 and day 1 are adjacent in reality and maximally distant as numbers. December and January are neighbours; as month numbers 12 and 1 they are far apart.

Encoding cyclical values as a pair of sine and cosine components preserves the wraparound so the model sees the end of the cycle as close to its beginning. It is a small change that removes a genuine class of error, particularly around year end.

Tree-based models are less sensitive to this than linear ones, but it still helps, and it costs nothing to include.

Holidays are harder than they look

A single is_holiday flag misses most of the effect. Retail demand shifts in the days before a holiday and slumps after; some holidays move each year; regional holidays differ within one country.

FeatureWhy it matters
Days until next holidayCaptures the build-up, not just the day
Days since last holidayCaptures the slump afterwards
Holiday typeA bank holiday and a religious festival differ
Regional applicabilityScotland and England do not share every date
Shifted trading daysA closed Monday moves demand to Saturday

Easter alone causes considerable trouble because it moves by several weeks between years, so anything keyed to week number mis-aligns.

Time zones and the traps that follow

Store timestamps in UTC and convert for feature extraction, not the other way round. Extracting hour of day from a UTC timestamp for a business operating across time zones produces a nonsensical daily pattern.

Daylight saving introduces two further oddities each year: a day with 23 hours and one with 25. If you aggregate by day and compare year on year, those two days will look anomalous for reasons nothing to do with your business.

A timestamp handed over raw is a rich field reduced to a counter.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

Which date feature usually helps most?

In customer models, time since the previous event - days since last order or last contact. In demand models, day of week and holiday proximity.

Do tree-based models need cyclical encoding?

Less than linear models, since they can split anywhere, but it still helps and costs nothing.

How do I handle multiple countries' holidays?

Keep a holiday table with country and region, and generate the features per record based on where it applies rather than using a single national calendar.

Should I include a trend or time index?

With care. It lets the model extrapolate a trend, which can be useful or can produce implausible values beyond the training range.

Keep reading

More on AI & Machine Learning

Start here

Want machine learning project details from us?

Tell us what you are trying to predict and roughly what data you hold. We will come back with an honest view on whether machine learning is the right tool, what the work would involve and a realistic cost range. If a spreadsheet would do the job, we will say so.

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →