This question already has answers here:
How can I pad a value with leading zeros?
(71个答案)
3年前关闭。
我需要在所有9个月的“第二天”文本框中的月份中添加前导零,并在10、11、12处将其保留。我不知道如何实现这一目标。
帮帮我!
(71个答案)
3年前关闭。
我需要在所有9个月的“第二天”文本框中的月份中添加前导零,并在10、11、12处将其保留。我不知道如何实现这一目标。
帮帮我!
$(document).ready(function(){
var currentDate = new Date()
var day = currentDate.getDate()
if(day <= 9)
day = '0'+day;
var month = currentDate.getMonth() + 1
if(month <= 9)
month = '0'+month;
var year = currentDate.getFullYear()
document.getElementById("element_183").value =("" + month + "/" + day + "/" + year + "")
function DateFromString(str){
str = str.split(/\D+/);
str = new Date(str[2],str[0]-1,(parseInt(str[1])+90));
return MMDDYYYY(str);
}
function MMDDYYYY(str) {
var ndateArr = str.toString().split(' ');
var Months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec';
return (parseInt(Months.indexOf(ndateArr[+1])/4)+1)+'/'+ndateArr[2]+'/'+ndateArr[3];
}
function Add90Days() {
var date = $('#element_183').val();
var ndate = DateFromString(date);
return ndate;
}
$('#element_183').ready(function(){
$('#element_184').val(Add90Days());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Enter date:<input type="text" id="element_183" readonly>
Next date:<input type="text" id="element_184" readonly>
最佳答案
尝试以下代码:
var currentDate = new Date();
//currentDate.setMonth(currentDate.getMonth() + 7);
var day = currentDate.getDate()
if(day <= 9)
day = '0'+day;
var month = currentDate.getMonth() + 1
if(month <= 9)
month = '0'+month;
var year = currentDate.getFullYear()
document.getElementById("element_183").value =("" + month + "/" + day + "/" + year + "")
function DateFromString(str){
str = str.split(/\D+/);
str = new Date(str[2],str[0]-1,(parseInt(str[1])+90));
return str;
}
function MMDDYYYY(str) {
var ndateArr = str.toString().split(' ');
var Months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec';
var str = new Date(str);
var day = str.getDate();
if(day <= 9)
day = '0'+day;
var month = str.getMonth() + 1
if(month <= 9)
month = '0'+month;
var year = str.getFullYear()
return ("" + month + "/" + day + "/" + year + "");
}
function Add90Days() {
var date = $('#element_183').val();
var ndate = MMDDYYYY(DateFromString(date));
return ndate;
}
$('#element_183').ready(function(){
$('#element_184').val(Add90Days());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Enter date:<input type="text" id="element_183" readonly>
Next date:<input type="text" id="element_184" readonly>
10-04 22:46