let unix_timestamp = 1549312452;

// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp * 1000);

// Hours part from the timestamp
var hours = date.getHours();

// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();

// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();

// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

console.log(formattedTime);

For more information regarding the Date object, please refer to MDN or the ECMAScript 5 specification.

Answer from Aron Rotteveel on Stack Overflow
Discussions

How can I convert "2020-11-18-05:12" into unix timestamp? and then add 5 hours to it?
Prerequisite: dealing with dates and times in programming is one of the most non-trivial things you can ever undertake. Until the JS standard library can handle date-time in a sane manner, seriously consider using a library like Luxon instead of trying to wrangle time on your own! You should generally allow the browser to calculate the local time, instead of changing the timestamp. Let's break down why. First, to get the ms from epoch: const dateTimeString = '2020-11-18-05:12' const [years, months, days, hours, minutes, seconds = 0, ms = 0] = dateTimeString.split(/[-:]/).map(n => +n) const unixDate = new Date(years, months - 1, days, hours, minutes, seconds, ms).valueOf() If you take that number, add 18,000,000 to it (5hrs in ms), then pass that number back into the Date constructor, you will find that the time is now wrong by 5 hours. This is because the Date constructor assumes all dates are entered in UTC, and will automatically use the timezone of the local machine to display the appropriate time (assuming that is what you are trying to accomplish). Also assuming that all you want is to display that time string in the user's local time, just remove the .valueOf() and you have a timezone appropriate Date object. As an aside, that string is almost an ISO 8601-1:2019 compliant date-time string, 2020-11-18T05:12:00, which can be converted directly with the Date constructor. Edit: A better way to offset the time, is to use a date-time+timezone string: const dateTimeString = '2020-11-18-05:12' const offset = '+05:00' const [years, months, days, hours, minutes, seconds = '00', ms = '000'] = dateTimeString.split(/[-:]/) const date = new Date(`${years}-${months}-${days}T${hours}:${minutes}:${seconds}.${ms}${offset}`) More on reddit.com
🌐 r/learnjavascript
4
1
November 18, 2020
javascript - How to convert date in format "YYYY-MM-DD hh:mm:ss" to UNIX timestamp - Stack Overflow
But when I check this using 'onlineconversion.com/unix_time.htm' its shows the timestamp converted date to Mon, 15 Aug 2011 07:48:52 GMT which is wrong. ... This code converts YYYY-MM-DD hh:mm:ss to YYYY/MM/DD hh:mm:ss that is easily parsed by Date constructor. More on stackoverflow.com
🌐 stackoverflow.com
August 22, 2015
javascript - how convert unixtime to yy-mm-dd - Stack Overflow
I've been searching how to convert UNIXTIME to YY-MM-DD format using javascript, but so far I've only found methods to do it the other way around (YY-MM-DD to UNIXTIME). Is there any method already More on stackoverflow.com
🌐 stackoverflow.com
January 5, 2023
node.js - Function to convert timestamp to human date in javascript - Stack Overflow
YYYY-MM-DD is parsed as UTC, all other date–only forms are implementation dependent and treated as local (probably). Read the spec. 2024-08-29T13:51:02.827Z+00:00 ... This works fine. Checked in chrome browser: var theDate = new Date(timeStamp_value * 1000); dateString = theDate.toGMTString(); ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Zipy
zipy.ai › blog › convert-unix-timestamp-to-date-and-time-in-javascript
convert unix timestamp to date and time in javascript
April 12, 2024 - These libraries offer extensive functionalities for date and time manipulation, including straightforward Unix timestamp conversions. ... const moment = require('moment'); const date = moment.unix(unixTimestamp).format("MM/DD/YYYY HH:mm:ss"); console.log(date); // Outputs: "12/04/2020 07:27:42"
🌐
UsefulAngle
usefulangle.com › post › 258 › javascript-timestamp-to-date-time
Convert Timestamp to Date & Time in Javascript - UsefulAngle
December 19, 2019 - // unix timestamp var ts = 1565605570; // convert unix timestamp to milliseconds var ts_ms = ts * 1000; // initialize new Date object var date_ob = new Date(ts_ms); // year as 4 digits (YYYY) var year = date_ob.getFullYear(); // month as 2 digits (MM) var month = ("0" + (date_ob.getMonth() + 1)).slice(-2); // date as 2 digits (DD) var date = ("0" + date_ob.getDate()).slice(-2); // hours as 2 digits (hh) var hours = ("0" + date_ob.getHours()).slice(-2); // minutes as 2 digits (mm) var minutes = ("0" + date_ob.getMinutes()).slice(-2); // seconds as 2 digits (ss) var seconds = ("0" + date_ob.getS
🌐
Sentry
sentry.io › sentry answers › javascript › convert unix timestamp to date and time in javascript
Convert Unix timestamp to date and time in JavaScript | Sentry
To convert these into human-readable dates and times, we can use JavaScript’s Date object. This object’s constructor takes a value similar to a Unix timestamp, but in milliseconds rather than seconds.
🌐
Epoch Converter
epochconverter.com
Epoch Converter - Unix Timestamp Converter
All examples return the epoch timestamp in seconds (and not milliseconds). The full list in the programming section also includes examples for converting human-readable dates to epoch time. ... Thanks to everyone who sent me corrections and updates! More date-related programming examples: What's the current week number? - What's the current day number? Please note: All tools on this page are based on the date & time settings of your computer and use JavaScript to convert times.
🌐
Nesin
nesin.io › blog › javascript-date-to-unix-timestamp
How to convert Date to Unix Timestamp in Javascript - Nesin.io
April 2, 2023 - And unix timestamp are mostly ... it by 1000. const date = new Date(); // Or any Date('YYYY-MM-DD') const unixTimestamp = Math.floor(date.getTime() / 1000);...
Find elsewhere
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss");
🌐
IQCode
iqcode.com › code › javascript › unix-timestamp-to-date-javascript-yyyy-mm-dd
unix timestamp to date javascript yyyy-mm-dd Code Example
January 21, 2015 - function timeConverter(UNIX_timestamp){ var a = new Date(UNIX_timestamp * 1000); var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul'...
🌐
Reddit
reddit.com › r/learnjavascript › how can i convert "2020-11-18-05:12" into unix timestamp? and then add 5 hours to it?
r/learnjavascript on Reddit: How can I convert "2020-11-18-05:12" into unix timestamp? and then add 5 hours to it?
November 18, 2020 -

so if I have a string that is in the format of 'yyyy-mm-dd-HH:MM' , then how can I convert that into a unix timestamp?

if I get the unix timestamp, then I could just add however many seconds there are in 5 hours and add it (assuming the unix timestamp is in seconds)

Top answer
1 of 3
1
Prerequisite: dealing with dates and times in programming is one of the most non-trivial things you can ever undertake. Until the JS standard library can handle date-time in a sane manner, seriously consider using a library like Luxon instead of trying to wrangle time on your own! You should generally allow the browser to calculate the local time, instead of changing the timestamp. Let's break down why. First, to get the ms from epoch: const dateTimeString = '2020-11-18-05:12' const [years, months, days, hours, minutes, seconds = 0, ms = 0] = dateTimeString.split(/[-:]/).map(n => +n) const unixDate = new Date(years, months - 1, days, hours, minutes, seconds, ms).valueOf() If you take that number, add 18,000,000 to it (5hrs in ms), then pass that number back into the Date constructor, you will find that the time is now wrong by 5 hours. This is because the Date constructor assumes all dates are entered in UTC, and will automatically use the timezone of the local machine to display the appropriate time (assuming that is what you are trying to accomplish). Also assuming that all you want is to display that time string in the user's local time, just remove the .valueOf() and you have a timezone appropriate Date object. As an aside, that string is almost an ISO 8601-1:2019 compliant date-time string, 2020-11-18T05:12:00, which can be converted directly with the Date constructor. Edit: A better way to offset the time, is to use a date-time+timezone string: const dateTimeString = '2020-11-18-05:12' const offset = '+05:00' const [years, months, days, hours, minutes, seconds = '00', ms = '000'] = dateTimeString.split(/[-:]/) const date = new Date(`${years}-${months}-${days}T${hours}:${minutes}:${seconds}.${ms}${offset}`)
2 of 3
1
I'd suggest using a library like date-fns because you'd be kinda insane to try this on your own. Date-time in JS is incredibly complicated to get right, so much so that we often relegate this to libraries instead of building our own. As for how to use date-fns, import { parse, add } from 'date-fns'; const dateTimeString = '2020-11-18-05:12'; const parsedDateTime = parse(dateTimeString, 'yyyy-mm-dd-HH:MM', new Date()); const nextDateTime = add(parsedDateTime, { hours: 5}); And that's about it.
🌐
Scaler
scaler.com › home › topics › convert timestamp to date in javascript
Convert Timestamp to Date in JavaScript - Scaler Topics
March 1, 2023 - The UNIX timestamps can be converted to the required date and time formats by using the Date() object and its methods in JavaScript.
🌐
Sling Academy
slingacademy.com › article › javascript-convert-timestamp-to-date-time-and-vice-versa
JavaScript: Convert timestamp to date time and vice versa - Sling Academy
February 19, 2023 - Display a Date object in 12-hour format UTC Time and Local Time Check if a date string is valid Convert Date Time to Time Ago Get current date time in yyyy/MM/dd HH:mm:ss format Subtract and Compare 2 Dates 2 Ways to Convert a String into a Date Object Convert Timestamp to Date Time and Vice Versa Check if 2 Date Ranges Overlap Add a duration to a Date object Truncate the time portion of a date string Clone a Date Object Sorting an Array of Objects by Date Property Get an array of dates between 2 given dates HTML native date picker User-Friendly Date Formatting in JavaScript Time Zone Handling
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript convert timestamp to date
How to Convert Unix Timestamp to Date in JavaScript | Delft Stack
February 12, 2024 - // Import Moment.js const moment ...nixTimestamp); // Convert to formatted date and time string const formattedDate = momentObject.format('MMMM Do YYYY, h:mm:ss a'); console.log(formattedDate);...
🌐
GitHub
gist.github.com › kmaida › 6045266
Convert a UNIX timestamp to user's local time via JavaScript · GitHub
var d = new Date(timestamp * 1000) just remove 1000 from new Date(timestamp * 1000), new Date(timestamp )
Top answer
1 of 2
1

You could use a library like moments.js or in POJS

Moments.js

A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates.

unixtime

Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[note 1] not counting leap seconds.[note 2] It is used widely in Unix-like and many other operating systems and file formats. Due to its handling of leap seconds, it is neither a linear representation of time nor a true representation of UTC.[note 3] Unix time may be checked on some Unix systems by typing date +%s on the command line.

Javascript Date object

Summary

Creates JavaScript Date instances which let you work with dates and times.

Javascript

function padZero(number) {
    if (number < 10) {
        number = "0" + number;
    }

    return number;
}

function unixtime2YYMMDD(unixtime) {
    var milliseconds = unixtime * 1000,
        dateObject = new Date(milliseconds),
        temp = [];

    temp.push(dateObject.getUTCFullYear().toString().slice(2));
    temp.push(padZero(dateObject.getUTCMonth() + 1));
    temp.push(padZero(dateObject.getUTCDate()));

    return temp.join("-");
}

console.log(unixtime2YYMMDD(1372069271));

Output

13-06-24 

On jsfiddle

2 of 2
1
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;

var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = yyyy.toString().substr(2,2)+'-'+mm+'-'+dd;
document.getElementById("output").innerHTML = today;

This will do the job ..

Of course your new Date() will be your unix time..

http://jsfiddle.net/UpMU5/

🌐
Codedamn
codedamn.com › news › javascript
How to convert timestamp to date in JavaScript?
November 3, 2022 - We write the following code to convert the timestamp value to a date object. const date = moment.unix("1666632563"); console.log(date);Code language: JavaScript (javascript)