Jason
Jason

Reputation: 52533

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.

Upvotes: 25

Views: 71491

Answers (6)

Bruno Bronosky
Bruno Bronosky

Reputation: 70369

Update

I just found my favorite method while adding the hacktastic bonus at the bottom.

new Date().toLocaleDateString('en', {weekday:'long'})
// returns "Thursday"
new Date().toLocaleDateString('en', {weekday:'short'})
// returns "Thu"
new Date().toLocaleDateString('es', {weekday:'long'})
// returns "jueves"
new Date().toLocaleDateString('es', {weekday:'short'})
// returns "jue."
new Date().toLocaleDateString('fr', {weekday:'long'})
// returns "jeudi"
new Date().toLocaleDateString('fr', {weekday:'short'})
// returns "jeu."

Original Answer

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"

Hacktastic Bonus... If you only care about the abbreviation, you can trim everything else.

new String(new Date()).replace(/ .*/,'')

// or
(''+new Date()).split(' ')[0]

// or
Date().slice(0,3)

Upvotes: 24

Gobinda Nandi
Gobinda Nandi

Reputation: 515

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;
    }

Upvotes: 0

Emil Stenström
Emil Stenström

Reputation: 14086

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

Upvotes: 9

Kip
Kip

Reputation: 109413

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

Upvotes: 57

geoyws
geoyws

Reputation: 3687

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.

Upvotes: 7

Nosredna
Nosredna

Reputation: 86216

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

Upvotes: 2

Related Questions