Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp to human readable format

Well I have a strange problem while convert from unix timestamp to human representation using javascript

Here is timestamp

1301090400 

This is my javascript

var date = new Date(timestamp * 1000); var year    = date.getFullYear(); var month   = date.getMonth(); var day     = date.getDay(); var hour    = date.getHours(); var minute  = date.getMinutes(); var seconds = date.getSeconds();   

I expected results to be 2011 2, 25 22 00 00. But it is 2011, 2, 6, 0, 0, 0 What I miss ?

like image 200
georgevich Avatar asked Mar 24 '11 09:03

georgevich


People also ask

What time function will convert the timestamp to a human readable format?

Use date() Function to Convert a Timestamp to a Date/Time in PHP.

How do I convert epoch time to human readable in Excel?

Select a blank cell, suppose Cell C2, and type this formula =(C2-DATE(1970,1,1))*86400 into it and press Enter key, if you need, you can apply a range with this formula by dragging the autofill handle. Now a range of date cells have been converted to Unix timestamps.


1 Answers

getDay() returns the day of the week. To get the date, use date.getDate(). getMonth() retrieves the month, but month is zero based, so using getMonth()+1 should give you the right month. Time value seems to be ok here, albeit the hour is 23 here (GMT+1). If you want universal values, add UTC to the methods (e.g. date.getUTCFullYear(), date.getUTCHours())

var timestamp = 1301090400, date = new Date(timestamp * 1000), datevalues = [    date.getFullYear(),    date.getMonth()+1,    date.getDate(),    date.getHours(),    date.getMinutes(),    date.getSeconds(), ]; alert(datevalues); //=> [2011, 3, 25, 23, 0, 0] 
like image 131
KooiInc Avatar answered Oct 05 '22 09:10

KooiInc