我希望用户打开网页和文本框显示当前星期五的值。我不知道如何使用.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"/>
答案 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)