0

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.

dr.bill.al
  • 33
  • 2
  • All you are asking is how to add two dates together and then how to use a date formatter string. NEVER write your own date displays like the 3 answers so far are doing. Always translate a DateTime() using a formatter, and store all dates in UTC or something consistent. – Stephen J Mar 08 '18 at 20:43
  • The reason you never use your own enum is for locale differences and maintenance. Look at the Non-accepted answers here to find out the right way, the accepted answer is a nightmare the moment it gets used in another country or calendar change: https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Stephen J Mar 08 '18 at 20:51

3 Answers3

2

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)
CRice
  • 29,968
  • 4
  • 57
  • 70
2

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.

Community
  • 1
  • 1
CRice
  • 29,968
  • 4
  • 57
  • 70
0

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()]);
yajiv
  • 2,901
  • 2
  • 15
  • 25