1

我有这个html:

<input class="smalldatepicker" type="date" id="datepickerFrom" value="2016-09-04">
</input>
<label class="cccsfont"> to </label>
<input type="date" class="smalldatepicker" id="datepickerTo" value="2016-09-10">
</input>

它将“datepickerTo”设置为最后一个星期六(这是基于今天 9 月 15 日星期四的日期),将“datepickerFrom”设置为之前的星期天。

不过,这是手动添加的;我想以编程方式在 jQuery 中执行此操作,例如:

var lastSat = getLastSaturday(); // return in form "YYYY-MM-DD" 
var sundayBeforeLastSat = getSundayBeforeLast(lastSat); // " "

$("#datepickerTo").val(lastSat);
$("#datepickerFrom").val(sundayBeforeLastSat);

我认为这应该可行,但我不知道在函数中添加什么:

function getLastSaturday() {
     return ???;
}

function sundayBeforeLastSat(lastSat) {
     return ???;
}
4

2 回答 2

2

如果您不想使用任何库,例如 moment.js 或其他

尝试这个 :

$(function(){

	
var lastSat = getLastSaturday(); // return in form "YYYY-MM-DD" 
var sundayBeforeLastSat = getSundayBeforeLastSat(lastSat); // " "
console.log("Last satuarday: "+lastSat);
    console.log("Last Sunday: "+sundayBeforeLastSat);

$("#datepickerTo").val(lastSat);
$("#datepickerFrom").val(sundayBeforeLastSat);
});

function getLastSaturday() {
    newDate= new Date();
	daysAfterSat = newDate.getDay()+1;
	lastSat = new Date(newDate.setDate(newDate.getDate()-daysAfterSat));
	dateString = lastSat.getFullYear()+ "-"+('0' + (lastSat.getMonth()+1)).slice(-2) + '-'+('0' + lastSat.getDate()).slice(-2);
              
	return dateString;
}

function getSundayBeforeLastSat(lastSat) {
    date = new Date(lastSat);
	lastSun = new Date(date.setDate(date.getDate()-6));
	dateString = lastSun.getFullYear()+ "-"+('0' + (lastSun.getMonth()+1)).slice(-2) + '-'+('0' + lastSun.getDate()).slice(-2);
	return dateString;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input class="smalldatepicker" type="date" id="datepickerFrom" value="">
</input>
<label class="cccsfont"> to </label>
<input type="date" class="smalldatepicker" id="datepickerTo" value="">
</input>
</form>

于 2016-09-15T19:27:18.820 回答
2

这是DateES6 中的操作。

jQuery可能有一些模块可以做到吗?

否则moment.js是一个很好的库,可以为你做吗?

function pad(num) {
  return `0${num}`.slice(-2);
}

// d is a Date object,returns 'YYYY-MM-DD'
function format(d) {
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
}

function day(d, dow) {
  d.setTime(d.getTime() + (dow - d.getDay()) * 86400000);
  return d;
}

// dateString is of form 'YYYY-MM-DD'
function parse(dateString) {
  const args = dateString.split('-');
  args[1] -= 1;
  return args;
}

function getLastSaturday(...dateArgs) {
  return format(day(new Date(...dateArgs), -1));
}

function getSundayBeforeLast(lastSat) {
  return format(day(new Date(...parse(lastSat)), 0));
}

const lastSat = getLastSaturday();
const sundayBeforeLastSat = getSundayBeforeLast(lastSat);

$('#datepickerTo').val(lastSat);
$('#datepickerFrom').val(sundayBeforeLastSat);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="smalldatepicker" type="date" id="datepickerFrom">
<label class="cccsfont">to</label>
<input type="date" class="smalldatepicker" id="datepickerTo">

并使用 moment.js

function getLastSaturday(...dateArgs) {
  return moment(dateArgs).day(-1).format('YYYY-MM-DD');
}

function getSundayBeforeLast(lastSat) {
  return moment(lastSat).day(0).format('YYYY-MM-DD');
}

const lastSat = getLastSaturday();
const sundayBeforeLastSat = getSundayBeforeLast(lastSat);

$('#datepickerTo').val(lastSat);
$('#datepickerFrom').val(sundayBeforeLastSat);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.js"></script>
<input class="smalldatepicker" type="date" id="datepickerFrom">
<label class="cccsfont">to</label>
<input type="date" class="smalldatepicker" id="datepickerTo">

于 2016-09-15T18:51:46.473 回答