10

Is it possible to get the day of the week from the onSelect event of the datepicker.

I know I can get the day of the month, but I want the day of the week. Even just the day index will be fine e.g. 0-6.

I know you can get it in the beforeShowDay (or similar) event, but I need to react differently depending on the day of the week chosen. It's to do with setting the opening times.

I could hit the server and find out, but that is not going to come back fast enough...ideally i want it all done on the client.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
SteveCl
  • 4,529
  • 6
  • 29
  • 38

3 Answers3

18

This should do the trick:

function(event, ui) {
    var date = $(this).datepicker('getDate');
    var dayOfWeek = date.getUTCDay();
};

This has the advantage of not needing to parse whatever arbitrary date format you've chosen for the Datepicker's text field - the getDate method returns a native Javascript Date object which you can then easily extract the date fields you need.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
6

Also, if you want the actual name of the day of the week (without using the getUTCDay index into a array of something like ['Sunday', 'Monday', ...], you can use the datepicker object itself to give this to you, like below

var curDate = $(this).datepicker('getDate');
var dayName = $.datepicker.formatDate('DD', curDate);

This can also be used to get a shortened day of the week name (M), long or short day of the month (MM or M) by substituting DD for whatever component you're after.

Bradley Bossard
  • 2,439
  • 2
  • 23
  • 30
3

I would onSelect convert the value of the textbox to be a javascript date and the use getUTCDay() to get the value of the day of the week. http://www.w3schools.com/jsref/jsref_getutcday.asp

Avitus
  • 15,640
  • 6
  • 43
  • 53