Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from unix timestamp to datetime

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; 
like image 240
Robin Wieruch Avatar asked Jun 07 '13 07:06

Robin Wieruch


People also ask

How do I convert timestamp to time in Unix?

To convert a date to a Unix timestamp:Create a Date object using the Date() constructor. Get a timestamp in milliseconds using the getTime() method. Convert the result to seconds by dividing by 1000 .

How do I change timestamp to datetime?

Timestamp to DateTime object You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

Is UTC Unix time?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC. One of the primary benefits of using Unix time is that it can be represented as an integer making it easier to parse and use across different systems.


2 Answers

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"); 
like image 179
Dai Avatar answered Sep 18 '22 15:09

Dai


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

like image 25
Blair Anderson Avatar answered Sep 18 '22 15:09

Blair Anderson