JQuery datepicker returns week number and day of week

I am trying to get the week number and name / shortcut of the day of the week according to the selected date via the jQuery calendar. I am not very good at jQuery, so I can get the week number, but I can not get the name of the day with it.

Can anyone help me out?

$('#datepicker').datepicker({ onSelect: function (dateText, inst) { $('#weekNumber').val($.datepicker.iso8601Week(new Date(dateText))); } }); 
+6
source share
3 answers

you need to get a date, than extract the name of the day from it:

  var date = $(this).datepicker('getDate'); alert($.datepicker.formatDate('DD', date)); 

hope this helps!

edit: you can find a working script here .

+6
source

You can add to your existing block to get the day name using the format date syntax found in the datepicker documentation, here: http://api.jqueryui.com/datepicker/#utility-formatDate

In this case, the full name of the day is obtained from "DD", so your updated code may look like this:

 $('#datepicker').datepicker({ onSelect: function (dateText, inst) { var d = new Date(dateText); $('#weekNumber').val($.datepicker.iso8601Week(d)); $('#dayName').val($.datepicker.formatDate('DD', d)); } }); 

Spell here: http://jsfiddle.net/duffmaster33/zL6m2wck/

+2
source

Here is a lot of information on how to use a Date javascript object.

Here is the code that I offer you:

  $(function() { $( "#datepicker" ).datepicker({ onSelect: function( dateText, dateObj ){ //You can get your date string that way console.log(dateText); //You can get a few value about the date, look in your console to see what you can do with that object //ie - console.log(dateObj.selectedDay) console.log(dateObj); //You can see the result of the date in string that way $('.string').append(dateText); currDate = new Date(dateText); //You can have a complete Date object using the Date javascript method console.log(currDate); //The Date object in javascript provides you all you need then //Get the number of day in a week, from 0(Sunday) to 6(Saturday) $('.getday').append(currDate.getDay()); //Create a function to see day Sun - Sat function getWeekDay(date) { var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] return days[ date.getDay() ] } //Then we use it $('.getweekday').append(getWeekDay(currDate)); } }); }); 

You can see my fiddle: https://jsfiddle.net/qapw32Lp/

Here is a great source of information that you can use also in the Date object: http://javascript.info/tutorial/datetime-functions

+1
source

Source: https://habr.com/ru/post/985273/


All Articles