Tutorial :Use jQuery/JS to determine the DAY OF WEEK


Is there a method using either JavaScript or jQuery to determine what day of the week it is? For instance, if the date a user picks in the box is a Sunday, I can alert them.



new Date().getDay();  //0=Sun, 1=Mon, ..., 6=Sat  

See also: Javascript Date Object on MDN.

Word of Caution: This returns day of week on the browser. Because the earth is not flat, the day of the week for some users might be different from the day of the week for your server. This may or may not be relevant to your project...

If you are doing a lot of date work, you may want to look into JavaScript date libraries like Datejs or Moment.js


If you only need this once in your page, keep it simple...

["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][(new Date()).getDay()]  

I didn't think it would be that big a deal to extend, but since I have 3 votes for internationalization in the comments...

// dictionary version  ({    es: ["Domingo", "Lunes",  "Martes",  "Miércoles", "Jueves",   "Viernes", "Sábado"],    en: ["Sunday",  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",  "Saturday"]  })['en'][(new Date()).getDay()]  // returns Wednesday    // list version  langs=['en','es']    [    ["Domingo", "Lunes",  "Martes",  "Miércoles", "Jueves",   "Viernes", "Sábado"],    ["Sunday",  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",  "Saturday"]  ][langs.indexOf('es')][(new Date()).getDay()]  // returns Miércoles  

And finally, the code golf version...

["Sun","Mon","Tues","Wednes","Thurs","Fri","Satur"][(new Date()).getDay()]+"day"  


today = new Date()  dayIndex = today.getDay()  

... will get you a numeric representation of "today".

0 = Sunday  1 = Monday  2 = Tuesday  3 = Wednesday  4 = Thursday  5 = Friday  6 = Saturday  


A clearer and more detailed answer:

var days = [      'SUN', //Sunday starts at 0      'MON',      'TUE',      'WED',      'THU',      'FRI',      'SAT'  ];    d = new Date(); //This returns Wed Apr 02 2014 17:28:55 GMT+0800 (Malay Peninsula Standard Time)  x = d.getDay(); //This returns a number, starting with 0 for Sunday    alert (days[x]);  

Working fiddle.


If you need a lot of date handling, take a look at DateJS.


send date and you will get position of the same day of that month . i.e. 15/02/2018 is Third Thursday.

 /*return day postion and dayname */      function weekAndDay(date) {          var arrayWeek =[];          var date = new Date(date),          days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],          prefixes = ['','First', 'Second', 'Third', 'Fourth', 'Fifth'];          if (date.getDate() % 7 == 0) {              var prefixes2 = date.getDate() / 7;          }else{              var prefixes2 = Math.ceil(date.getDate() / 7);          }          arrayWeek.push({              'dayPos' : prefixes[prefixes2] ,              'dayName' : days[date.getDay()]          });          return arrayWeek;      }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »