I'm trying get name of the day of the week like "Wednesday" 2 years and 30 days later from now.
i am stuck here, and i already tried :
var ts=(new Date()).getDay();
console.log(new Date(ts));
thanks for your help.
I'm trying get name of the day of the week like "Wednesday" 2 years and 30 days later from now.
i am stuck here, and i already tried :
var ts=(new Date()).getDay();
console.log(new Date(ts));
thanks for your help.
You could do something like this:
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
var c = new Date(year + 2, month, day + 30)
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[c.getDay()];
document.write(n)
Sounds like your looking for the getDay
method of the Date
object.
Take a look at the docs here.
const now = new Date();
console.log("now:", now.toISOString())
// 2 years and 30 days later from now
const later = new Date(now.getFullYear() + 2, now.getMonth(), now.getDate() + 30);
console.log("later:", later.toISOString())
const days = ["Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"];
console.log(later.getDay(), days[later.getDay()])
And an important note on that method:
Date.prototype.getDay()
Returns the day of the week (0-6) for the specified date according to local time.
Emphasis mine. So if this is running on the client, the day of week will be in their local time, not UTC.
Try this
var weekday =
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var someDate = new Date();
var numberOfDaysToAdd = 30;
var numberOfyearsToAdd = 2;
someDate.setDate(someDate.getFullYear() + numberOfyearsToAdd +
someDate.getDate() + numberOfDaysToAdd);
console.log(weekday[someDate.getDay()]);