Select week number of selected date using jquery datepicker

I have the following code to select the date, which I then want to convert to the week number.

$(".calendar").datepicker({ showWeek: true, onSelect: function(dateText, inst) { dateFormat: "'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)), alert($.datepicker.iso8601Week(new Date(dateText))) } }); 

The warning shows the correct week number, but the date is not reformatted at all.

I cannot transfer the dateFormat function beyond onSelect, because it does not mean anything.

What I would like to get is a text box to say something like "Week Number 13"

Any ideas?

http://jsfiddle.net/ENG66/

+4
source share
3 answers

I updated the script to make it work: http://jsfiddle.net/ENG66/11/

In the onselect event, you need to use .val () to override the value of the text field value

 $(".calendar").datepicker({ showWeek: true, onSelect: function(dateText, inst) { $(this).val("'Week Number '" + $.datepicker.iso8601Week(new Date(dateText))); } });​ 

EDIT

As Igor Shastin noted in his comment below, we already have a text box in inst.input

 inst.input.val($.datepicker.iso8601Week(new Date(dateText))); 
+6
source

Try the jsFiddle example .

 $(".calendar").datepicker({ showWeek: true, onSelect: function(dateText, inst) { dateFormat: "'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)), $(this).val('Week:' + $.datepicker.iso8601Week(new Date(dateText))); } });​ 
+2
source

The answers above suggest that the date format can be processed using Javascript. In the case of short European dates (dd / mm / yyyy, unlike the American mm / dd / yyyy), you can get the date value through the datepicker itself, which will process the conversion automatically.

 $("#week_field").val( $.datepicker.iso8601Week( $("#date_field").datepicker( "getDate" ) )); 

You can set a different date format for datepicker as follows

 $.datepicker.setDefaults( {dateFormat : "dd/mm/yy"} ); 
0
source

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


All Articles