Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: get month/year/day from unix timestamp

I have a unix timestamp, e.g., 1313564400000.00. How do I convert it into Date object and get month/year/day accordingly? The following won't work:

function getdhm(timestamp) {
        var date = Date.parse(timestamp);
        var month = date.getMonth();
        var day = date.getDay();
        var year = date.getYear();

        var formattedTime = month + '/' + day + '/' + year;
        return formattedTime;

    }
like image 383
Yang Avatar asked Feb 28 '12 00:02

Yang


People also ask

How do I convert a timestamp to a month?

Use the MONTH() function to retrieve a month from a date/datetime/timestamp column in MySQL. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a date/datetime/timestamp column. (In our example, we use the start_date column of date data type).

How do I read a Unix timestamp?

To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch.

How do I convert time stamps to dates?

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.


1 Answers

var date = new Date(1313564400000);
var month = date.getMonth();

etc.

This will be in the user's browser's local time.

like image 129
Jim Blackler Avatar answered Oct 08 '22 23:10

Jim Blackler