0

使用 jQuery 从 datepicker 中选择后,我必须在输入框中显示日期。我使用了以下代码:

$(function () {
    $("#datepicker").datepicker({
        minDate: -0,
        maxDate: "+1M +2D",
        showOn: "button",
        buttonImage: "calendar.png",
        buttonImageOnly: true,
        dateFormat: 'D dd MM' });
    $.datepicker.formatDate('yy-mm-dd');
});

而且我能够以这种格式获取日期:“Fri 09 August”。但是我的要求是:8 月 9 日星期五(今天)

这是一个可以使用的 JS Fiddle:http: //jsfiddle.net/4cgPe/

知道我怎么能做到这一点吗?

4

2 回答 2

0

我找不到太多关于向 jQuery 添加自定义文本的信息,所以我想出了自己的 JavaScript。我建立在您的 DatePicker 代码之上。

(我的理解是,如果选择的日期是今天,则添加“(今天)”,或者,如果选择的日期是明天,则添加“(明天)”,或者,其他日期不添加任何内容。

<html><head><link type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js" ></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" ></script>
<script type="text/javascript">
$(function () {
    $("#datepicker").datepicker({
        minDate: -0,
        maxDate: "+1M +2D",
        showOn: "button",
        buttonImage: "calendar.png",
        buttonImageOnly: true,
        dateFormat: 'D dd MM yy' });
    $.datepicker.formatDate('yy-mm-dd');
});

function updateDate(){
    var inputDate=document.getElementById('datepicker').value; 
    var selectedDate=new Date(inputDate);
    var today=new Date();
    var tomorrow=new Date();
    tomorrow.setDate(today.getDate()+1);
    selectedDate.setHours(0,0,0,0);
    today.setHours(0,0,0,0);
    tomorrow.setHours(0,0,0,0);
    var DaysFull=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
    var DaysShort=["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
    var MonthsFull=["January","February","March","April","May","June","July","August","September","October","November","December","January"];
    var MonthsShort=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan"];
    var finalDate=DaysShort[selectedDate.getDay()]+" "+selectedDate.getDate()+" "+MonthsFull[selectedDate.getMonth()];
    if(selectedDate.getTime() == today.getTime()){

        document.getElementById('datepicker').value=finalDate+" (Today)";
    }
    else if(selectedDate.getTime() == tomorrow.getTime()){document.getElementById('datepicker').value=finalDate+" (Tomorrow)";}
    else {document.getElementById('datepicker').value=finalDate;}
}

</script>
</head>
<body>
<form><input type="text" id="datepicker" onchange="javascript:updateDate()" /></form>
</body></html>

编辑:

附加功能:可能需要使用全天/月名称或短天/月名称,因此您可以相应地使用数组。

DaysFull : 周日、周一等

DaysHalf : 太阳、星期一等

MonthsFull : 一月、二月等

MonthsHalf:一月、二月等

希望这可以帮助。如果这还不够,请告诉我。:)

于 2013-08-09T20:40:16.327 回答
0

不同类型格式的完整源代码如下:

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery UI Datepicker - Format date</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/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.3/jquery-ui.js"></script>

    <script>
    $(function() {
        $( "#datepicker" ).datepicker();
        $( "#format" ).change(function() {
            $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
        });
    });
    </script>
</head>
<body>

<p>Date: <input type="text" id="datepicker" size="30" /></p>

<p>Format options:<br />
    <select id="format">
        <option value="mm/dd/yy">Default - mm/dd/yy</option>
        <option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
        <option value="d M, y">Short - d M, y</option>
        <option value="d MM, y">Medium - d MM, y</option>
        <option value="DD, d MM, yy">Full - DD, d MM, yy</option>
        <option value="'day' d 'of' MM 'in the year' yy">With text - 'day' d 'of' MM 'in the year' yy</option>
    </select>
</p>


</body>
</html>

有关格式的更多信息请参阅此链接

于 2013-08-09T11:45:12.737 回答