我想得到日期和月份的名称

时间:2016-04-30 07:17:51

标签: javascript jquery datepicker

我希望获得日期和月份的名称以下脚本正常工作,但现在我想显示所选日期的日期名称和月份名称。

$(document).ready(function() {
    $("#departing").datepicker({ 
        dateFormat: "yy-mm-dd", 
        onSelect: function() {
            var m_names = ["January", "February", "March", 
                "April", "May", "June", "July", "August", "September", 
                "October", "November", "December"];

            var selected = $(this).val(),
                selectedToArray = selected.split('-'),
                year = selectedToArray[0],
                month = selectedToArray[1],
                day = selectedToArray[2];

            $('#date').text(year);
            $('#month').text(month);
            $('#day').text(day);
        } 
    });
});

2 个答案:

答案 0 :(得分:0)

var m_names = ["January", "February", "March",
  "April", "May", "June", "July", "August", "September",
  "October", "November", "December"
];
var month = 0;
console.log(m_names[month])//get the month name from array base on index you can use the data picker month value for this. As sample i put 0 for January

进一步的样本

var m_names = ["January", "February", "March",
  "April", "May", "June", "July", "August", "September",
  "October", "November", "December"
];

var now = new Date();
var month = now.getMonth();
console.log(month)//3 
console.log(m_names[month])//using js new date. where month is current month
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

试试这个:

<script type="text/javascript"> 
$( document ).ready(function() {
$("#departing").datepicker(  { 
dateFormat: "yy-mm-dd", 
onSelect: function() {

var m_names = ["January", "February", "March", 
                "April", "May", "June", "July", "August", "September", 
                "October", "November", "December"];

   // get day of week:
    var seldate = $(this).datepicker('getDate');
    seldate = seldate.toDateString();
    seldate = seldate.split(' ');
    var weekday=new Array();
        weekday['Mon']="Monday";
        weekday['Tue']="Tuesday";
        weekday['Wed']="Wednesday";
        weekday['Thu']="Thursday";
        weekday['Fri']="Friday";
        weekday['Sat']="Saturday";
        weekday['Sun']="Sunday";
     var dayOfWeek = weekday[seldate[0]];
     alert(dayOfWeek)



  var 
    selected = $(this).val(),
    selectedToArray = selected.split('-'),
    year = selectedToArray[0],
    month = selectedToArray[1],
    day = selectedToArray[2];

    month = month.replace(/^0+/, ''); // add this line to remove leading zero , so we can ferch proper value from array
    $('#date').text(year);
    $('#month').text(m_names[month]); // access with m_names[month]
    $('#day').text(day);
 } 
 });
});
</script>