duanhao4909
duanhao4909
2017-09-20 08:46

在jquery datepicker中的onselectiong日期,我需要在表单文本字段中获取日期和月份

on selecting date, it has to display day name like Sunday,Monday and month name like January,February in other fields.

thanks in advance,

here is my html code

<input type="text" id="datepicker" class="form-control"></p>

<input type="text" id="day" class="form-control"></p>
<input type="text" id="month" class="form-control"></p>

here is my js

<script>
    $( "#datepicker" ).datepicker({dateFormat: 'dd-mm-yy'});
    $('#btn').click(function(){
         var _myDate = new Date($('#datepicker').datepicker("option" , 
"dateFormat", "mm-dd-yy").val());
         var monthNames = ["January", "February", "March", "April", "May", 
 "June","July", "August", "September", "October", "November", "December"];

          $('#my_month').val(monthNames[_myDate.getMonth()]);
          $('#my_day').val(_myDate.getDay());
    });
    </script>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dth2331 dth2331 4年前

    $( function() {
    var weekday=new Array(7);
    weekday[0]="Sunday";
    weekday[1]="Monday";
    weekday[2]="Tuesday";
    weekday[3]="Wednesday";
    weekday[4]="Thursday";
    weekday[5]="Friday";
    weekday[6]="Saturday";
    
            $( "#datepicker" ).datepicker({
                dateFormat: 'mm-dd-yy',
                inline: true,
                onSelect: function(dateText, inst) {
                    var date = $(this).datepicker('getDate'),
                        day  = date.getDate(),
                        month = date.getMonth() + 1,
                        year =  date.getFullYear();
     var dayOfWeek = weekday[date.getUTCDay()+1];
                    $("#day").val(dayOfWeek);
                    $("#month").val(month);
                }
    
            });
    
        } );
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    
    <input type="text" id="datepicker" class="form-control">
    
    <input type="text" id="day" class="form-control">
    <input type="text" id="month" class="form-control">

    </div>
    
    点赞 评论 复制链接分享