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.
Related videos on Youtube
Author by
Pidoaisn
Updated on June 04, 2022Comments
-
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 almost 13 yearsplease post whatever you have tried or have you?
-
Roland Illig almost 13 yearsSurprisingly, 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 almost 13 yearsHow 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 over 9 yearsWrong - gives the numeric day not the "day of the week".
-
klidifia over 9 yearsWrong - gives the numeric day not the "day of the week"
-
ViES almost 5 yearsshorter
(new Date(1557964800000)).getDay();