input date标签默认值今天(jquery)

彭鸿哲
2023-12-01
 // 给input  date设置默认值
var now = new Date();
//格式化日,如果小于9,前面补0
var day = ("0" + now.getDate()).slice(-2);
//格式化月,如果小于9,前面补0
var month = ("0" + (now.getMonth() + 1)).slice(-2);
//拼装完整日期格式
var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
//完成赋值
$('#id_yy_input').val(today);


--------------------- 
作者:qq_39161501 
来源:CSDN 
原文:https://blog.csdn.net/qq_39161501/article/details/79113121 

附加:设置前一个月:

var end = new Date();
var day = ("0" + end.getDate()).slice(-2);
var month = ("0" + (end.getMonth() + 1)).slice(-2);
var today = end.getFullYear()+"-"+(month)+"-"+(day) ;
$('#enddate').val(today);

var start = new Date();
start.setMonth(start.getMonth()-1);
day = ("0" + start.getDate()).slice(-2);
month = ("0" + (start.getMonth() + 1)).slice(-2);
today = start.getFullYear()+"-"+(month)+"-"+(day) ;
$('#startdate').val(today);

 

 类似资料: