1

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 ?

Sir
  • 8,135
  • 17
  • 83
  • 146
  • [There are many ways to format a `Date` object](http://stackoverflow.com/a/12632212/1048572) – Bergi May 16 '13 at 20:32
  • what is `data[i].postedon` ? – karthikr May 16 '13 at 20:32
  • @karthikr It says in the question – Sir May 16 '13 at 20:34
  • @Bergi i'm not trying to use a library however :) – Sir May 16 '13 at 20:39
  • 1
    A few notes on your existing code: 1) If you are dividing by `1000`, then `parseFloat` is effectively implied, making the `parseInt` useless. 2) Even if it weren't useless, you should ALWAYS pass a base as a second parameter, almost always `10`. 3) If `data[i].postedon` is the number of seconds, you should be *multiplying* by `1000`, not dividing. – Niet the Dark Absol May 16 '13 at 20:41
  • Ah good point :) Thanks for the extra tips! – Sir May 16 '13 at 20:44
  • and 4) Do not assign to `document.write`, but call it like `document.write(postedon)`! – Bergi May 16 '13 at 20:56

1 Answers1

2

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);
};
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Did not know it was local-aware! Thanks for that i will test it out now :) – Sir May 16 '13 at 20:36
  • +1, but seriously, `months` and `days` should be arrays, not . . . this. – ruakh May 16 '13 at 20:37
  • @ruakh A year ago, I was using arrays. Then I decided I couldn't be bothered to hit `','` so many times, nor go find an old copy to copy-paste, so I did this XD Saves memory, though, which is neat. – Niet the Dark Absol May 16 '13 at 20:39
  • @Kolink: At the very least, you could write something like `"JanFebMarAprMayJunJulAugSepOctNovDec".match(/.../g)`, or `"Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec".split(";")`. This is no more verbose than what you've got, when you consider that it will let you write `[m]` instead of `.substr(m*3,3)`. – ruakh May 16 '13 at 20:45
  • Ooh, I like that first one. – Niet the Dark Absol May 16 '13 at 20:47