如何更改日期输出格式

时间:2015-07-15 04:39:42

标签: javascript date

我正在开发一个关于PHP,MySQL中的Allowance的系统,它应该计算最后一个日期员工来获得他们的津贴。这是我用来计算最后日期的JavaScript。以下代码的输出为 15-7-2015 。 我希望 15-07-2015

我应该改变什么来获得这样的输出?

<script type="text/javascript">
function calculateLast() {

	var x = document.form1.tempoh.value; //<-----------Tempoh Elaun
    var CurrentDate = new Date();
    CurrentDate.setMonth(CurrentDate.getMonth() + eval(x));

    var day = CurrentDate.getDate();
    var monthIndex = CurrentDate.getMonth()+1;
    var year = CurrentDate.getFullYear();

	document.getElementById('tamatElaun').value = day + "-" + monthIndex + "-" + year;
    //document.write(day, monthNames[monthIndex], year);
}
</script>

4 个答案:

答案 0 :(得分:0)

试试这个

 var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    } 
    var today = dd+'-'+mm+'-'+yyyy;
    document.getElementById("tamatElaun").value = today;

答案 1 :(得分:0)

试试我的代码

&#13;
&#13;
var CurrentDate = new Date('2015-7-15');
var listMonths = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
var day = CurrentDate.getDate();
var monthIndex = CurrentDate.getMonth();
var year = CurrentDate.getFullYear();
alert(day + '/' + listMonths[monthIndex] + '/' + year);
&#13;
&#13;
&#13;

您还可以将 listMonths 更改为字符串数组,例如:[&#39; Jan&#39;,&#39; 2月&#39;,...,&#39; 12月& #39]

答案 2 :(得分:0)

请尝试这样,

ArrayList()

答案 3 :(得分:0)

 var CurrentDate = new Date();
    CurrentDate.setMonth(CurrentDate.getMonth());

    var day = CurrentDate.getDate();
    var monthIndex = CurrentDate.getMonth()+1;
    if(monthIndex<10){
        monthIndex=('0'+monthIndex);
    }
    var year = CurrentDate.getFullYear();
    alert(monthIndex);
    //document.getElementById('tamatElaun').value = day + "-" + monthIndex + "-" + year;
    });