在文本框中显示当前星期五的值

时间:2014-12-22 17:26:19

标签: php jquery

我希望用户打开网页和文本框显示当前星期五的值。我不知道如何使用.val 这是我的代码。

$(function() {
$("#dateInput").datepicker({

    //In Datepicker set the Calendar display start with Sunday (by default datepicker starts from Sunday)
    firstDay: 0,

    //Before Populating the Calendar set the Enabled & Disabled Dates using beforeShowDay(Date) function
    beforeShowDay: function (date) {

        //Get today's date
        var sunday = new Date();

        //Set the time of today's date to 00:00:00 
        sunday.setHours(0,0,0,0);

        sunday.setDate(sunday.getDate() - (sunday.getDay() || 0));

        //Set the Date to Sunday
        var saturday = new Date(sunday);
        var thrusday = new Date(sunday);
        var currentday = new Date();

        //0 is sunday 6 is saturday
        var currentdayofweek = currentday.getUTCDay();
        //console.log(currentday, "show currentday");
        //alert('value of currentday=>'+currentday);
        //alert('value of currentday of week=>'+currentdayofweek);
        //location.reload();

        //Now add 6 to Sunday to get the Date of Saturday (End of that Week)
        saturday.setDate(sunday.getDate() + 6);
        //add by kim
        thrusday.setDate(sunday.getDate() + 4);

        //edit by kim
        //return [(date >= thrusday && date <= saturday ), ''];
        // current day of week <5  mean user can select friday only day before friday
        return [(date > thrusday && date < saturday && currentdayofweek < 5), ''];
    }
});
  var temp = $("#dateInput").datepicker("option", "dateFormat", "yy-mm-dd");

  return temp;
});

这是文本框代码。

<input type="text" name="dateInput" id="dateInput"/>
  1. 我只希望用户只能在日历中选择当周的星期五
  2. 当前星期五的值应在用户选择
  3. 之前显示在文本框中

2 个答案:

答案 0 :(得分:0)

这将在#dateInput输入中填入与当周星期五相对应的日期。

var date = new Date(),
    day = date.getDay(),
    dif = 5 - day >= 0 ? 5 - day : day - 5;
date.setTime( date.getTime() + (dif * 24 * 60 * 60 * 1000) );
$(document).ready(function() {
    $("#dateInput").val( date.getUTCFullYear() + "/" + (date.getMonth()+1) + "/" + date.getUTCDate() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="dateInput" id="dateInput"/>

答案 1 :(得分:0)

马尔文特斯。我的意思是格式是正确的,但日期不是文本框中显示的当前星期五。 例子。用户在2015年1月3日星期六登录系统,系统应在下周五为用户输入。它应该是2015/1/9。但您的代码输入值到文本框是2015/1/4