import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
dayjs.extend(timezone)

const datetime = dayjs.unix(1653134400)

console.log(datetime.tz('UTC').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 12:00:00 +00:00
console.log(
    datetime.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss Z') // 2022-05-21 05:00:00 -07:00
)
console.log(datetime.tz('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 20:00:00 +08:00
Answer from Alex G on Stack Overflow
🌐
Day.js
day.js.org › docs › en › parse › unix-timestamp
Unix Timestamp (seconds) · Day.js
Create a Day.js object from a Unix timestamp (10 digits, seconds since the Unix Epoch). ... This is implemented as dayjs(timestamp * 1000), so partial seconds in the input timestamp are included.
🌐
Day.js
day.js.org › docs › en › display › unix-timestamp
Unix Timestamp · Day.js
dayjs('2019-01-25').unix() // 1548381600 (Returns Unix timestamp based on local timezone.) This value is floored to the nearest second, and does not include a milliseconds component.
🌐
Day.js
day.js.org › docs › en › parse › date
Date · Day.js
var d = new Date(2018, 8, 18) var day = dayjs(d) This clones the Date object. Further changes to the Date won't affect the Day.js object, and vice-versa. ← Unix Timestamp (seconds)Object →
Top answer
1 of 3
2
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
dayjs.extend(timezone)

const datetime = dayjs.unix(1653134400)

console.log(datetime.tz('UTC').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 12:00:00 +00:00
console.log(
    datetime.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss Z') // 2022-05-21 05:00:00 -07:00
)
console.log(datetime.tz('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 20:00:00 +08:00
2 of 3
1

You have to include a couple of extender libraries for both UTC and Timezone formatting to work in dayjs. Both the UTC and Timezone Javascript files are required . I used version 1.10.7 because those were the most up to date.

If you're working in a browser include the following source. HTML:

<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script src="https://unpkg.com/[email protected]/plugin/utc.js"></script>
<script src="https://unpkg.com/[email protected]/plugin/timezone.js"></script>

JavaScript:

dayjs.extend(window.dayjs_plugin_utc);
dayjs.extend(window.dayjs_plugin_timezone);

let printFormat = 'hh:mm:ssA';
let nowLocal = dayjs().utc().local().format(printFormat);
console.log(nowLocal);
console.log(dayjs().tz("America/New_York").format(printFormat));
console.log(dayjs().tz("Asia/Tokyo").format(printFormat));

A couple of notes for your attempt, the 'tz' (Timezone) object does not hang off the unix() function, it is attached directly to the top level dayjs object. The above example takes the local time, and prints it in three different time zones each in the format hh:mm:ssAM|PM . The unix function takes an Unix timestamp and returns a DayJS object.

Here's a JSFiddle with the above example running: https://jsfiddle.net/e4x507p3/2/

Note, if you're using extensions in a browser, this should work, there is a different method required if you're working in NodeJS: https://day.js.org/docs/en/plugin/loading-into-nodejs

🌐
Day.js
day.js.org › docs › en › display › format
Format · Day.js
Get the formatted date according to the string of tokens passed in. To escape characters, wrap them in square brackets (e.g. [MM]). dayjs().format() // current date in ISO8601, without fraction seconds e.g.
🌐
Day.js
day.js.org › docs › en › parse › now
Now · Day.js
Calling `dayjs()` without parameters returns a fresh Day.js object with the current date and time.
🌐
Epochconverter
epochconverter.io › blogs › nodejs-timestamp-conversions
Mastering Timestamp Conversions in Node.js with dayJs
1 2 3 4 5 6 7const dayjs = ... // Output: '2022-03-15 00:21:00'In this example, we create a dayjs object from the Date object and then use the format() method to format the date in the desired format....
🌐
Day.js
day.js.org › docs › en › display › unix-timestamp-milliseconds
Unix Timestamp (milliseconds) · Day.js
dayjs('2019-01-25').valueOf() // 1548381600000 +dayjs(1548381600000) // 1548381600000 · To get a Unix timestamp (the number of seconds since the epoch) from a Day.js object, you should use Unix Timestamp.
🌐
Day.js
day.js.org › docs › en › parse › string-format
String + Format · Day.js
If you know the format of an input string, you can use that to parse a date. This requires the CustomParseFormat plugin to work · dayjs.extend(customParseFormat) dayjs("12-25-1995", "MM-DD-YYYY")
Find elsewhere
🌐
GitHub
github.com › dayjs › dayjs-website › blob › master › docs › parse › unix-timestamp.md
dayjs-website/docs/parse/unix-timestamp.md at master · dayjs/dayjs-website
Create a Day.js object from a Unix timestamp (10 digits, seconds since the Unix Epoch). ... This is implemented as dayjs(timestamp * 1000), so partial seconds in the input timestamp are included.
Author   dayjs
🌐
Day.js
day.js.org › docs › en › display › from-now
Time from now · Day.js
dayjs.extend(relativeTime) dayjs('1999-01-01').fromNow(true) // 22 years · The base strings are localized by the current locale and can be customized. Time is rounded to the nearest second.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-date-time-dayjs
JavaScript Dates – How to Use the DayJS Library to work with Date and Time in JS
August 28, 2023 - How you can utilise the DayJS library ... functions available within the DayJS library ... You can use the Date object in JavaScript to work with dates and periods of time....
🌐
GitHub
github.com › iamkun › dayjs › issues › 1218
Convert js date object to dayjs date object? · Issue #1218 · iamkun/dayjs
November 14, 2020 - Right now I am getting this returned - 11/13/2020, 11:56:48 PM but I want that to be a dayjs date object · var targetDate = new Date() // Current date/time of user computer var timestamp = targetDate.getTime()/1000 + targetDate.getTimezoneOffset() ...
Author   TMHDesign
🌐
Snyk
snyk.io › advisor › dayjs › functions › dayjs.unix
How to use the dayjs.unix function in dayjs | Snyk
export const unixTsToString = ts => { let dateString = dayjs.unix(ts).format("MMM D, YYYY"); return dateString; }; OriginProtocol / origin / dapps / marketplace / src / components / EventTick.js View on Github · const displayEventDate = timestamp => displayDateTime(dayjs.unix(timestamp), { day: 'numeric', month: 'short', year: 'numeric' }) const sentenceCase = str =>
🌐
OpenReplay
blog.openreplay.com › working-with-dates-and-times-with-day-js
Working with Dates and Times with Day.js
Without a suffix: You can supply the return date string with a boolean value of true. dayjs.extend(window.dayjs_plugin_relativeTime); dayjs('2000-01-01').fromNow(true) // Output: 23 years · This gives back a string representing a RelativeTime to the present. Keep in mind that this depends on the RelativeTime plugin. dayjs.extend(window.dayjs_plugin_relativeTime); dayjs('1980-01-01').toNow() // Output: in 43 years ... This gives the Day.js object’s Unix timestamp, or the number of seconds since the Unix Epoch.
🌐
Day.js
day.js.org › docs › en › display › as-javascript-date
As Javascript Date · Day.js
To get a copy of the native `Date` object parsed from the Day.js object use `dayjs#toDate`.
🌐
ZetCode
zetcode.com › javascript › dayjs
Day.js Tutorial - Simplifying Date and Time
The example creates date and time objects in three different ways. ... We create a dayjs object by parsing a string. let d2 = dayjs.unix(1530471537); console.log(d2.format()); We use a unix time stamp (in seconds) to define a datetime object.
🌐
Formio
formio.github.io › core › docs › classes › index.dayjs.dayjs.html
Dayjs | @formio/core
To get a copy of the native Date object parsed from the Day.js object use dayjs#toDate.
🌐
GitHub
github.com › iamkun › dayjs
GitHub - iamkun/dayjs: ⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
It's easy to use Day.js APIs to parse, validate, manipulate, and display dates and times. dayjs('2018-08-08') // parse dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // display dayjs().set('month', 3).month() // get & set dayjs().add(1, 'year') // manipulate dayjs().isBefore(dayjs()) // query
Starred by 48.6K users
Forked by 2.4K users
Languages   JavaScript
🌐
DEV Community
dev.to › sarahokolo › format-dates-and-times-easily-in-your-application-using-dayjs-fja
Easily handle dates and times operations in your application using Day.js - DEV Community
January 13, 2025 - const date1 = dayjs(new Date()); // called with native Date object. const date2 = dayjs("2024-08-15"); // called with an ISO date string const date3 = dayjs(1665613200000); // called with a Unix timestamp · The formatting feature allows you to display dates and times in a specific format.