Pidoaisn
Pidoaisn

Reputation: 103

Getting day of the week from timestamp using JavaScript

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

Upvotes: 10

Views: 25474

Answers (4)

Raymond Keating
Raymond Keating

Reputation: 19

Similar to klidifia's answer, but for some reason the day of the week was off. I had to update the 'days' array to start on Monday.

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

Upvotes: 0

Teneff
Teneff

Reputation: 32158

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

Upvotes: 12

klidifia
klidifia

Reputation: 1495

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.

Upvotes: 29

Ammu
Ammu

Reputation: 5127

Try out the following:

var currentTime = new Date();
var day = currentTime.getDate();

Upvotes: -2

Related Questions