使用javascript添加前导零?

时间:2017-03-21 16:32:59

标签: javascript jquery

我需要在"第二天"上添加一个前导零。所有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());
    });

});
&#13;
<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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

我将使用this answer中使用的技术,因此对该用户赞不绝口。

var currentDate = new Date('01/15/2017');
var currentDay = ('0' + currentDate.getDate()).slice(-2);
var currentMonth = ('0' + (currentDate.getMonth() + 1)).slice(-2);
// This is not just relevant to dates!
var number = 3;
var paddedNumber = ('0' + number).slice(-2);

// currentDay = '15'
// currentMonth = '01'
// number = '03'

.slice(-2)做的是执行&#34;子串&#34;,但是从字符串的末尾开始,切断前面。这样,如果您已经有2位数的日期或月份,它将变为3个字符,然后第一个将被删除。

答案 1 :(得分:0)

我一直在测试这个问题,我觉得它是这样的:

$(document).ready(function(){

 var currentDate = new Date()
    var day = currentDate.getDate()
    if(day <= 9)
    var stringDay = '0'+day.toString();
    var month = currentDate.getMonth() + 1
    if(month <= 9)
    var stringMonth = '0'+month.toString();
    var year = currentDate.getFullYear()
    document.getElementById("element_183").value =("" + stringMonth + "/" + stringDay + "/" + 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());
    });

});

答案 2 :(得分:0)

请尝试以下代码:

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>