from unix timestamp to datetime
Asked Answered
L

9

34

I have something like /Date(1370001284000+0200)/ as timestamp. I guess it is a unix date, isn't it? How can I convert this to a date like this: 31.05.2013 13:54:44

I tried THIS converter for 1370001284 and it gives the right date. So it is in seconds.

But I still get the wrong date for:

var substring = unix_timestamp.replace("/Date(", "");
substring = substring.replace("000+0200)/", "");
var date = new Date();
date.setSeconds(substring);
return date;
Liquidation answered 7/6, 2013 at 7:15 Comment(4)
Did your code generate the timestamp value, or is it outside of your control? I ask because 1370001284000+0200 is not a valid timestamp because it looks like miliseconds, not seconds, and contains zone information.Ceporah
Out of my control. Because of that I am not really sure what it is. But I know that 1370001284000+0200 and 31.05.2013 13:54:44 matches.Liquidation
Assuming you've got your substring as "1370001284000" (the time in milliseconds from the epoch) you could just do var date = new Date( parseInt( substring, 10 ) );Racketeer
The best answer without using momentjs https://mcmap.net/q/434657/-from-unix-timestamp-to-datetimeBreezeway
C
44

Note my use of t.format comes from using Moment.js, it is not part of JavaScript's standard Date prototype.

A Unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC.

The presence of the +0200 means the numeric string is not a Unix timestamp as it contains timezone adjustment information. You need to handle that separately.

If your timestamp string is in milliseconds, then you can use the milliseconds constructor and Moment.js to format the date into a string:

var t = new Date( 1370001284000 );
var formatted = moment(t).format("dd.mm.yyyy hh:MM:ss");

If your timestamp string is in seconds, then use setSeconds:

var t = new Date();
t.setSeconds( 1370001284 );
var formatted = moment(t).format("dd.mm.yyyy hh:MM:ss");
Ceporah answered 7/6, 2013 at 7:20 Comment(7)
For seconds the year is somethign like 2056. So it has to be miliseconds. I tried var date = new Date(1370001284000); but it says invalid date. Edit: I think the timestamp is a bit weird, because if I do something like timestamp * 100, I get a valid date but for the year 4500 or so.Liquidation
You need to strip the +0200 part from the string, convert to a number, then supply it as an argument to the Date( miliseconds ) constructor.Ceporah
@Ceporah t.format is not a function does it require Moment.js?Translatable
@Translatable I was quoting from the OP who seems to be using Moment.js as you pointed out.Ceporah
My timestamp is in UTC format. Any change I need to make here for that? How can I have am/pm?Ensemble
Second example doesn't work, as it adds the number of seconds to the current date, which is near year 2065.Fadein
yes, you can convert unix timestamp to date using Moment js, here is complete tutorial for this coderszine.com/convert-timestamp-to-date-using-javascript. You can also convert it using online tools epochconvert.comDioscuri
B
20

Looks like you might want the ISO format so that you can retain the timezone.

var dateTime = new Date(1370001284000);
dateTime.toISOString(); // Returns "2013-05-31T11:54:44.000Z"

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

Breezeway answered 6/8, 2014 at 17:54 Comment(0)
F
8

Without moment.js:

var time_to_show = 1509968436; // unix timestamp in seconds

var t = new Date(time_to_show * 1000);
var formatted = ('0' + t.getHours()).slice(-2) + ':' + ('0' + t.getMinutes()).slice(-2);

document.write(formatted);
Fadein answered 6/11, 2017 at 12:0 Comment(0)
G
3

The /Date(ms + timezone)/ is a ASP.NET syntax for JSON dates. You might want to use a library like momentjs for parsing such dates. It would come in handy if you need to manipulate or print the dates any time later.

Gumboil answered 7/6, 2013 at 8:57 Comment(0)
J
3

If using react:

import Moment from 'react-moment';
Moment.globalFormat = 'D MMM YYYY';

then:

<td><Moment unix>{1370001284}</Moment></td>
Jimenez answered 14/2, 2018 at 4:47 Comment(2)
it is not working, getting the wrong outputDittany
in my case i tried with out unix on< Moment >{}</moment>Dittany
A
2

Import moment js:

var fulldate = new Date(1370001284000);
var converted_date = moment(fulldate).format(");
Adalia answered 16/11, 2016 at 20:44 Comment(1)
You're adding an answer to a question which is three years old. It's generally best if you explain (in the answer) why you feel this answer is better than the existing answers. Otherwise it is likely to be downvoted or removed altogether.Zerk
D
2

if you're using React I found 'react-moment' library more easy to handle for Front-End related tasks, just import <Moment> component and add unix prop:

import Moment from 'react-moment'

 // get date variable
 const {date} = this.props 

 <Moment unix>{date}</Moment>
Doorjamb answered 31/8, 2020 at 10:4 Comment(0)
B
1

I would like to add that Using the library momentjs in javascript you can have the whole data information in an object with:

const today = moment(1557697070824.94).toObject();

You should obtain an object with this properties:

today: {
  date: 15,
  hours: 2,
  milliseconds: 207,
  minutes: 31,
  months: 4
  seconds: 22,
  years: 2019
}

It is very useful when you have to calculate dates.

Butterfat answered 15/5, 2019 at 0:35 Comment(1)
how do you convert the month to something like April instead of 4?Dittany
G
1

for people as dumb as myself, my date was in linux epoch but it was a string instead of an integer, and that's why i was getting RangeError: Date value out of bounds

so if you are getting the epoch from an api, parseInt it first

var dateTime = new Date(parseInt(1370001284000));
dateTime.toISOString();
Gmt answered 11/10, 2022 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.