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;
Robin Wieruch
  • 14,900
  • 10
  • 82
  • 107
  • 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. – Dai Jun 07 '13 at 07:19
  • 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. – Robin Wieruch Jun 07 '13 at 07:24
  • 1
    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 ) );` – Matijs Jun 07 '13 at 08:50
  • The best answer without using momentjs https://stackoverflow.com/a/25166955/1536309 – Blair Anderson Aug 15 '18 at 20:05

9 Answers9

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");
Chris
  • 6,331
  • 1
  • 21
  • 25
Dai
  • 141,631
  • 28
  • 261
  • 374
  • 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. – Robin Wieruch Jun 07 '13 at 07:45
  • 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. – Dai Jun 07 '13 at 07:53
  • 23
    @Dai `t.format is not a function` does it require [Moment.js](http://momentjs.com/)? – mu3 Nov 14 '14 at 22:17
  • @mu3 I was quoting from the OP who seems to be using Moment.js as you pointed out. – Dai Nov 14 '14 at 23:29
  • My timestamp is in UTC format. Any change I need to make here for that? How can I have am/pm? – codec Oct 17 '16 at 09:27
  • Second example doesn't work, as it adds the number of seconds to the current date, which is near year 2065. – Rauli Rajande Nov 06 '17 at 11:42
  • yes, you can convert unix timestamp to date using Moment js, here is complete tutorial for this https://coderszine.com/convert-timestamp-to-date-using-javascript/. You can also convert it using online tools http://epochconvert.com – Laeeq Jul 11 '18 at 14:21
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

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
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);
Rauli Rajande
  • 2,010
  • 1
  • 20
  • 24
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.

Tommi Komulainen
  • 2,830
  • 1
  • 19
  • 16
3

If using react:

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

then:

<td><Moment unix>{1370001284}</Moment></td>
taggartJ
  • 277
  • 2
  • 6
2

Import moment js:

var fulldate = new Date(1370001284000);
var converted_date = moment(fulldate).format(");
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 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. – Heretic Monkey Nov 16 '16 at 20:57
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>
HiLuLiT
  • 433
  • 4
  • 7
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.

J C
  • 731
  • 7
  • 11
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();