Getting day of the week from timestamp using JavaScript

18,633

Solution 1

var timestamp = 654524560; // UNIX timestamp in seconds
var xx = new Date();
xx.setTime(timestamp*1000); // javascript timestamps are in milliseconds
document.write(xx.toUTCString());
document.write(xx.getDay()); // the Day

2020 Update

If this browser support is acceptable for you you can use this one liner:

new Date(<TIMESTAMP>).toLocaleDateString('en-US', { weekday: 'long' }); // e.g. Tuesday

Solution 2

var timestamp = 1400000000;
var a = new Date(timestamp*1000);
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var dayOfWeek = days[a.getDay()]

Now the "day of the week" is in the dayOfWeek variable.

Share:
18,633

Related videos on Youtube

Pidoaisn
Author by

Pidoaisn

Updated on June 04, 2022

Comments

  • Pidoaisn
    Pidoaisn almost 2 years

    How do I get the day of the week from a timestamp in JavaScript? I'd like to get this from a timestamp I specify, not the current date.

    Thanks

    • Sangeet Menon
      Sangeet Menon almost 13 years
      please post whatever you have tried or have you?
    • Roland Illig
      Roland Illig almost 13 years
      Surprisingly, in this case the w3schools page is indeed a nice starting point. That doesn't mean one should refer to that web site in other situations.
    • Thor Jacobsen
      Thor Jacobsen almost 13 years
      How come not? I know it's not a complete source of knowledge, but my experience is, that it's a good place to start, before venturing out on the vast fields of Google...
  • klidifia
    klidifia over 9 years
    Wrong - gives the numeric day not the "day of the week".
  • klidifia
    klidifia over 9 years
    Wrong - gives the numeric day not the "day of the week"
  • ViES
    ViES almost 5 years
    shorter (new Date(1557964800000)).getDay();

Related