如何计算 jQuery 中的日期并将输入日期的值设置为该值?
How can I calculate dates in jQuery, and set an input date's value to that?
我有这个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 ???;
}
这是 ES6 中的 Date
操作。
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">
如果您不想使用任何库,例如 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>
我有这个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 ???;
}
这是 ES6 中的 Date
操作。
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">
如果您不想使用任何库,例如 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>