2

我有一个简单的日期选择器文本字段:

<input type="text" name="book_on" id="book_on" value="<?php echo $today; ?>"

jQuery如下:

$('#book_on').datepicker({
        dateFormat: 'mm/dd/y'
      });

This is all very straight-forward, but I also need to show the chosen date in a different format elsewhere on the page - specifically, when a date is chosen, I need to show which day of the week it is in a <span>on the page .

这将是dateFormat: 'D'但我找不到让日期选择器返回第二种日期格式的方法。我不需要编写整个代码,如果您可以使用上面的函数来提醒所选日期的星期几,那就太好了。

4

4 回答 4

7

聚会有点晚了,但我相信真正的解决方案是利用两个日期选择器属性

    altField: "#your-span",
    altFormat: "D",

设置这些选项将自动#your-span以正确的格式填充元素。无需额外代码!

于 2012-05-15T17:34:12.800 回答
3

您是否正在寻找类似jsFiddle 示例的内容?

jQuery:

var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$('#book_on').datepicker({
    dateFormat: 'mm/dd/y',
    onSelect: function(event, ui) {
        var dayOfWeek = $(this).datepicker('getDate').getUTCDay();
        $('#day').val(days[dayOfWeek]);
    }
});​
于 2012-05-15T16:07:40.953 回答
2

您可以使用onSelect事件并这样做:

var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
$("#book_on").datepicker({
    onSelect: function(dateText, inst) {
        var day = new Date(dateText).getDay();
        $("span").text(weekday[day]);
    }
});​

演示:http: //jsfiddle.net/LXCMD/

于 2012-05-15T16:09:05.840 回答
0

这将是 dateFormat: 'D' 但我找不到让日期选择器返回第二种日期格式的方法。

Datepicker 插件有一个实用功能,专门用于以您想要的任何格式输出日期,例如

var t = $('#picker').datepicker('getDate');
var myDate = $.datepicker.formatDate('D', t);

因此:

$('#book_on').datepicker({
    dateFormat: 'mm/dd/y',
    onSelect: function(dateText, inst) {
        var date = $(this).datepicker('getDate');
        $('#span').text($.datepicker.formatDate('D', date));
    }
});​
于 2012-05-15T16:12:25.583 回答