Sir
Sir

Reputation: 8277

Return a string from milliseconds since epoch

I have this variable which is to hold a string for a time/date.

The problem is that, it also shows timezone which i don't want. So this is what i have:

//data[i].posted has seconds sinch epoch
var postedon = new Date( parseInt(data[i].postedon/1000) );
document.write = postedon;

The result is i get for example:

Thu Jan 01 1970 00:00:00
GMT+0000 (GMT Standard Time)

Thing is i don't want the GMT+0000 (GMT Standard Time)

How can i filter that out ?

Upvotes: 1

Views: 94

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324710

Date.prototype.toString (which is what you are effectively calling here) is locale-aware. That means someone in France would see something similar to jeudi 1er janvier 1970 01:00:00 GMT+1

In other words, you have absolutely no control.

You can, however, either define your own function or override the built-in one. Try something like this:

Date.prototype.toString = function() {
    var y = this.getUTCFullYear(),
        m = this.getUTCMonth(),
        d = this.getUTCDate(),
        h = this.getUTCHours(),
        i = this.getUTCMinutes(),
        s = this.getUTCSeconds(),
        w = this.getUTCDay(),
        months = "JanFebMarAprMayJunJulAugSepOctNovDec",
        days = "SunMonTueWedThuFriSat",
        pad = function(n) {return n<10?'0'+n:n;};
    return days.substr(w*3,3)+" "+months.substr(m*3,3)+" "+pad(d)+" "+y+" "+pad(h)+":"+pad(i)+" "+pad(s);
};

Upvotes: 2

Related Questions