从Javascript Datepicker中获取工作日名称

时间:2014-02-15 14:21:53

标签: javascript jquery datepicker

我有一个代码片段,它给出了从JSDatepicker库的Calender中选择的日期。

我正在寻找一种方法来获取工作日的名称,选择日期为:星期一,星期二等......

以下是jsDatepicker的使用情况:

<script type="text/javascript">
    window.onload = function(){
        new JsDatePick({
            useMode:2,
            target:"inputField1",
            dateFormat:"%Y-%m-%d",
            cellColorScheme:"beige"             
        });

    };
</script>

我检查了jsDatePicker中给出的文件,但没有得到任何结果。 有人可以帮忙吗?

我也尝试过使用jQuery Datepicker,但它根本不起作用,下面是jQuery datepicker代码:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script>    
$(function() {
    $( "#datepicker" ).datepicker();
});
</script>

对于这两种情况,我选择和显示日期的输入字段为:

<input type="text" id="datepicker"/>

2 个答案:

答案 0 :(得分:1)

如果你想使用JsDatePick,这是一种方法:

// Save returning object to variable
var dateSelector = new JsDatePick({
    useMode:2,
    target:"inputField1",
    dateFormat:"%Y-%m-%d",
    cellColorScheme:"beige"             
});

// Use setOnSelectedDelegate to capture clicks
dateSelector.setOnSelectedDelegate(function(){
    // Get selected day from plug-in
    var day = dateSelector.getSelectedDay();

    // Create a JS native Date
    var date = new Date(day.year, day.month, day.day);

    // Get the day of the week (0 is Sunday)
    var dayOfTheWeek = date.getDay();

    // For demonstration purposes, place a string into input field
    var inputField = document.getElementById("inputField1");
    var dayOfTheWeekStr = '';

    switch (dayOfTheWeek) {
        case 0:
            dayOfTheWeekStr = 'Sunday';
            break;
        case 1:
            dayOfTheWeekStr = 'Monday';
            break;
        case 2:
            dayOfTheWeekStr = 'Tuesday';
            break;
        default:
            dayOfTheWeekStr = 'Wed.-Sat.';
            break;
    }

    inputField.value = dayOfTheWeekStr;
});

这是JSFiddle

答案 1 :(得分:1)

对于jquery datepicker,这里是fiddle

$(function() {
    var dt = $("#datepicker" ).datepicker({
            onSelect: function (dt) {
                var wa = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
                var nd = new Date(Date.parse(dt));
                $(this).val(wa[nd.getDay()]+', '+dt);
            }
    });

});