将Date.now()拆分为Javascript

时间:2015-08-09 15:56:49

标签: javascript html

我正在尝试将当前日期分成几部分,这就是我正在做的事情

        var today = new Date();

        var currDay = today.getDay();
        var currMonth = today.getMonth();
        var currYear = today.getYear();
        alert(currDay + "/" + currMonth + "/" + currYear)

但是我得到了以下结果

        0/7/115 (output)

应该在哪里

        09/07/2015

我可以做些什么来获得所需的结果?

5 个答案:

答案 0 :(得分:0)

尝试

var currYear = today.getFullYear();

答案 1 :(得分:0)

基于https://stackoverflow.com/a/3605248/2649340的回答:

var today = new Date();

var output   = ('0' + today.getDate()).slice(-2) + '/'
             + ('0' + (today.getMonth()+1)).slice(-2) + '/'
             + today.getFullYear();

答案 2 :(得分:0)

你的“today.getDay()”应该是“today.getDate()”,因为.getDay从0到6(星期几)

var today = new Date();
var currDay = today.getDate();
var currMonth = today.getMonth();
var currYear = today.getFullYear();
alert(currDay + "/" + currMonth + "/" + currYear);

答案 3 :(得分:0)

getDay()为您提供当天的数字(周日至周日为0-6),月份从0开始为1月。这是怎么做的:

var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();

if(day<10) {
    day='0'+day;
} 

if(month<10) {
    month='0'+month;
} 

today = month+'/'+day+'/'+year;
alert(today);

答案 4 :(得分:0)

您可以使用moment.js。它在JavaScript中有很多有用的时间和日期函数