Check if the date selected is javascript weekend

Asked

Viewed 1,226 times

2

I have an input on my system that receives a date selected by the user, I would like to know how I can check if this date is a weekend.

my input works that way:

<input type="text" class="form-control" id="data" name="data" placeholder="Data" required>  

$( function() {
  $( "#data" ).datepicker({showAnim: "slideDown", dateFormat: 'dd/mm/yy' , autoSize: true ,maxDate: "0" , minDate: "-2m", 

                                   dayNamesMin: [ "Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab" ],
                                   dayNamesShort: [ "Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab" ],
                                   monthNamesShort: [ "Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez" ],
                                   monthNames: [ "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"]});

  $("#data").on("change",function(){
        var selected = $(this).val();
        Edit(selected);});  
} );


  • If the intention is to block the selection of days on weekends, its component datepicker you will surely have an option.

1 answer

3


First you have to create a Date type object by watching the Timezone (GMT -0300) for Brasilia time, if applicable.

var Minha_data = new Date('Fri May 17 2019 11:20:58 GMT-0300');

Or in a more simplified way

var Minha_data = new Date(2019,4,17);

The months go from 0-11 and so for the month of May we use the 4.

Then you should use the getDay() method to pick up the day of the week (0-6) to know whether or not it is weekend. Where 0 is Sunday and 6 is Saturday.

var e_fds = Minha_data.getDay() == 0 || Minha_data.getDay() == 6 ? true : false

Follow link to check the result https://jsfiddle.net/rmwbyzg5/

  • It worked that way, thank you very much!

  • @Romulo depending on the importance of this information, do not forget to validate also on the server side.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.