Get a valid `datetime` attribute for the HTML `<time>` element (and others).





GitHub Stars



Last Commit

2mos ago








Type Definitions







Get a valid datetime attribute for HTML <time> (among others).

Lightweight (~ 1 KB compressed and tree-shakeable), datetime-attribute covers the whole datetime specification in 4 functions:

Node.js CI

Table of contents

Summary usage

import { datetime, datetimeTz, duration, tzOffset } from 'datetime-attribute'

const now = new Date()

datetime(now)                   // '2021-03-14'
datetime(now, 'time')           // '10:29'
datetimeTz(now, 'datetime', -7) // '2021-03-14T10:29-07:00'
utc(now, 'time')                // '09:29Z'

tzOffset(-9, 30) // '-09:30' (Marquesas Islands)
duration({ d: 4, h: 3, m: 17 }) // 'P4DT3H17M'


npm install datetime-attribute

Import the functions you need in your script:

// if you only need `datetime`
import { datetime } from 'datetime-attribute'

// if you need all functions
import { datetime, datetimeTz, duration, tzOffset } from 'datetime-attribute'

Not a NPM users? Copy/paste the code in your project.

Expressing moments with datetime()

datetime() accepts two optional arguments: a Date object, and a precision keywords.

import { datetime } from 'datetime-attribute'

const now = new Date() // We’re 14 March 2021 and it’s 10:29 in Brussels.

datetime(now)          // '2021-03-14'
datetime(now, 'local') // '2021-03-14T10:29'

Without argument, it defaults to today:

datetime() // today formatted in YYYY-mm-dd
datetime((new Date()), 'day') // same

Available precision keywords

By default, datetime() precision is day, resulting in a YYYY-mm-dd output. Many other values are available.


precisionexample outputdescription
day2021-03-14the default, fitting a calendar
year2021only the year
yearless03-14a day in a month
month2021-03a month in a year
week2021W10the week number (ISO-8601 spec) and its year

Time and UTC time


precisionexample outputdescription
time10:29hours and minutes, like most clocks
second10:29:00time with precision up to seconds
ms10:29:00.000time with precision up to milliseconds

To get UTC time, add utc to the time keyword:

precisionexample outputdescription
time utc09:29Ztime, shifted to UTC time
second utc09:29:00Zsecond, shifted to UTC time
ms utc09:29:00.000Zms, shifted to UTC time

Datetime and UTC datetime


precisionexample outputdescription
datetime2021-03-14T10:29a local datetime (= date + time separated by T)
datetime second2021-03-14T10:29:00time with precision up to seconds
datetime ms2021-03-14T10:29:00.000time with precision up to milliseconds

To get UTC datetime, add utc to the datetime keyword:

precisionexample outputdescription
datetime utc2021-03-14T09:29Zdatetime, shifted to UTC time
datetime second utc2021-03-14T09:29:00Zdatetime second, shifted to UTC time
datetime ms utc2021-03-14T09:29:00.000Zdatetime ms, shifted to UTC time

The utc shortcut

💡 Instead of adding utc to a time or datetime keyword, you can use utc(date, precision), which has datetime as default precision:

import { datetime, utc } from 'datetime-attribute'

const now =  new  Date()  // We’re 14 March 2021 and it’s 10:29 in Brussels.

// These are the same:
utc(now, 'time') // `09:29Z`
datetime(now, 'time utc') // `09:29Z`

// These are the same:
utc(now)             // `2021-03-14T09:29Z`
utc(now, 'datetime') // `2021-03-14T09:29Z`
datetime(now, 'datetime utc') // `2021-03-14T09:29Z`

Expressing timezone offsets with tzOffset()

Timezone offsets are a comparison against UTC time. For example, +01:00 means “one hour ahead of UTC time” and -05:00 means “five hours behind UTC time”.

tzOffset() accepts three optional arguments for hours, minutes, and compliance to real-life boundaries. Without argument, the local timezone offset is returned (and may differ based on daylight saving time).

import { tzOffset } from 'datetime-attribute'

tzOffset(3)      // '+03:00' (Moscow)

tzOffset(-9, 30) // '-09:30' (Marquesas Islands)
tzOffset(-9.5)   // '-09:30' (same with 1 parameter)

tzOffset(0)      //      'Z' (Ghana; 'Z' is equal to '+00:00')

// in Belgium
tzOffset()       // '+01:00'
tzOffset()       // '+02:00' (under daylight time saving)

Real-life timezone offset

The timezone offset will be adjusted to fit in the spec range (from -23:59 to +23:59). This means tzOffset(44) will output +20:00 instead of +44:00.

However, timezone offsets of countries in the world are all between -12:00 and +14:00. If you want tzOffset(44) to output -04:00 so that it matches real-life boundaries, give it a third parameter (default: false):

tzOffset(44) // '+20:00'
tzOffset(44, 0, true) // '-04:00'

Curious about timezones? Have a look at the timezone map and the daylight time saving chaos.

Adding a timezone offset to a moment with datetimeTz()

As datetime() doesn’t care about timezones, you can use datetimeTz() when you need to be explicit about the timezone of a moment.

💡 datetimeTz() is basically a concatenation of datetime(date, precision) and tzOffset(hours, minutes), so be sure to read about them.

It accepts the same 5 parameters, all optional:

datetimeTz(date, precision, offsetHours, offsetMinutes, inRealLifeBoundaries)
  1. A date object (default: new Date())
  2. A precision keywords among:
    • time
    • second
    • ms
    • datetime (default)
    • datetime second
    • datetime ms
  3. Hours offset like in tzOffset()
  4. Minutes offset like in tzOffset()
  5. Boundaries of the timezone offset like in tzOffset()

When hours and minutes are not specified, the local timezone offset is used.

import { datetime, datetimeTz } from 'datetime-attribute'

const now = new Date() // We’re 2 April 2021 and it’s 23:51 in Brussels.

datetime(now)   // '2021-04-02'
datetimeTz(now) // '2021-04-02T23:51+02:00'

datetime(now, 'time')           // '23:51'
datetime(now, 'time utc')       // '21:51Z' (same as previous, converted to UTC)
datetimeTz(now, 'time', 0)      // '23:51Z' (datetimeTz does not convert)
datetimeTz(now, 'time')         // '23:51+02:00' (fall back on local timezone)
datetimeTz(now, 'time', 9)      // '23:51+09:00'
datetimeTz(now, 'time', -3, 30) // '23:51-03:30'
datetimeTz(now, 'time', -14, 0, true) // '23:51+10:00'

datetimeTz() does not convert your moment to another timezone: it only adds the wanted timezone to the moment. Its purpose is to generate a valid datetime attribute saying “here’s a moment, it has this [hours, minutes] timezone offset”.

Let’s take this sentence and its HTML:

When I’m in Brussels, I wake up at 8 o’clock every day.

<p>When I’m in Brussels, I wake up <time datetime="08:00+02:00">at 8 o’clock</time> every day.</p>

Here’s how you can get the datetime attribute fitting this sentence:

// const awakeningAt = new Date(…) // a Date object with 08:00 as time

datetimeTz(awakeningAt, 'time', 2) // '08:00+02:00'

Expressing durations with duration()

duration() requires an object with entries for different levels of durations, from seconds to weeks. It also accepts a second parameter to control the conversion of units overflow (default: true).

import { duration } from 'datetime-attribute'

const countdownBeforeBigParty = {
  w: 3,   //     3 weeks
  d: 5,   //     5 days
  h: 10,  //    10 hours
  m: 43,  //    43 minutes
  s: 2.61 // 2.610 seconds

duration(countdownBeforeBigParty) // 'P3W5DT10H43M2'

All object keys are optional:

duration({ h: 17 }) // 'PT17H'

Units overflow

Values exceeding a unit are converted to upper units:

duration({ h: 31, m: 63, s: 175 }) // 'P1DT8H5M55S'

If you don’t need this behaviour, pass false as second parameter (default valut: true).

duration({ m: 175 }) // 'PT2H55M'
duration({ m: 175 }, false) // 'PT175M'


See CHANGELOG.md or the releases.

Browser and tooling support

datetime-attribute is provided for modern browsers usage with standard JavaScript syntax:

  • it is up to you to transpile it for legacy browsers;
  • you can’t import it using require('datetime-attribute').

If you’d like one of those features, feel free to open an issue and/or a PR that won’t have any side effects for modern usage. Read more about ESModules.

Not only in <time>

<time> is not alone! Other elements can benefit from datetime-attribute:


The datetime-attribute package is open-sourced software licensed under the DWTFYWTPL.

Rate & Review

Great Documentation0
Easy to Use0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Unwelcoming Community0