[removed] get month/year/day from unix timestamp

2019-02-16 11:06发布

问题:

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;

    }

回答1:

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

etc.

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



回答2:

Instead of using parse, which is used to convert a date string to a Date, just pass it into the Date constructor:

var date = new Date(timestamp);

Make sure your timestamp is a Number, of course.