53

How can I convert seconds into a datetime object in javascript.

Examples:

1.3308313703571

1.6324722385401

This is from a series of points and when they occurred. I understand 1.23323 more then seconds, but I can not change the value, being pulled from an api.

3
  • What kind of datetime do these floating point numbers represent?
    – deceze
    Commented Jan 6, 2011 at 4:32
  • These are a series of seconds, starting from zero. It is just a series of points represented by when they occurred.
    – jhanifen
    Commented Jan 6, 2011 at 4:34
  • 2
    I think the problem we're having here is that your examples that you want to convert do not make sense. Is that supposed to be the number of seconds from some specific point in time (1.3 seconds after Jan 1, 1970?)
    – Alex Vidal
    Commented Jan 6, 2011 at 4:41

6 Answers 6

100

You can try like this:

function toDateTime(secs) {
    var t = new Date(1970, 0, 1); // Epoch
    t.setSeconds(secs);
    return t;
}

Info on epoch date.

3
  • 7
    In my case, this answer does not give the correct answer because the timezone info seems strange. I tried @ariscris 's solution, change "t.setSeconds(secs);" to "t.setTime(secs * 1000)" gives me the correct result.
    – hengyue li
    Commented Nov 19, 2018 at 11:03
  • 1
    This solution gets affected by system timezone, new Date(1970, 0, 1).toISOString() outputs 1969-12-31T21:00:00.000Z (GMT+0300 in my case). setSeconds also is said to be in a local time and does not give the expected result even if base date is actually correct UTC epoch start.
    – Klesun
    Commented Jan 25, 2019 at 21:30
  • 4
    new Date(0) sets the date to 1st Jan, 1970 in local time. So t.setSeconds(secs) gets to current time in local time. That works perfectly well if secs is the UTC timestamp in seconds (usually sent by server). Commented Feb 25, 2019 at 8:36
84

You can pass unix timestamp milliseconds as an argument to the Date constructor:

const secs = 30;
const output = new Date(secs * 1000);

console.log(output);

2
  • 3
    This answer is much cleaner !
    – Lafi
    Commented Apr 5, 2020 at 16:46
  • 1
    In my case it adds extra 2 hours time zone shift
    – Niki
    Commented Jul 27, 2021 at 19:52
6

@UVM's answer is helpful, but slightly incomplete if you're dealing with timezones (i.e. UTC vs local time). With timezones, start with UTC using Date.UTC and Date.setUTCSeconds to get a true UTC date and time.

function toDateTime(secs) {
    var t = new Date(Date.UTC(1970, 0, 1)); // Epoch
    t.setUTCSeconds(secs);
    return t;
}

You can then use a library like Moment to convert/format it to a local timezone.

1
  • Or just return new Date(Date.UTC(1970, 0, 1, 0, 0, secs));
    – Albin
    Commented Jul 2 at 20:23
3

I dunno how it be 10 years ago, but now it can solve just doing next:

let sec = 1628618888939
let time = new Date(sec)
let normalDate = new Date(sec).toLocaleString('en-GB',{timeZone:'UTC'})
time: "Tue Aug 10 2021 21:08:08 GMT+0300 (Eastern European Summer Time)"
normalDate: "10/08/2021, 18:08:08"

If in the future u will have problems like this, I can advise read about functions that relate to your question, and solution will come.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

1

your example values have a decimal.. looking like you are trying to convert 1.something seconds into a date..

Meanwhile check this example here on the correct seconds to date conversion.. you could view their js sources.

1
  • 4
    Thanks, this works. Here's the code: var d = new Date(); d.setTime(secs * 1000);
    – ariscris
    Commented Jan 23, 2014 at 3:03
1

The question seems to have already been answered but this may be helpful for those attempting to do something similar to ruby's Time.at() method.

function formatDateTime(input){
       var epoch = new Date(0);
       epoch.setSeconds(parseInt(input));
       var date = epoch.toISOString();
       date = date.replace('T', ' ');
       return date.split('.')[0].split(' ')[0] + ' ' + epoch.toLocaleTimeString().split(' ')[0];
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.