-1

我已经尽力了,但没有得到结果。我需要的是 YYYY-MM-DD 格式的日期。这是我当前的代码:

function AddDays(days) {

            var thisDate = new Date();
            thisDate.setDate(thisDate.getDate() + days);
            var dd = ( thisDate.getFullYear() + '-' + (thisDate.getMonth() + 1) + '-' + thisDate.getDate() );
            return dd;

        }

        alert(AddDays(365));

结果我得到“2014-8-7”,但我想得到它作为 YYYY-MM-DD。我需要它看起来像“2014-08-07”。

4

9 回答 9

1

您需要检查日期或月份是否小于 10,如果是,则在日期/月份前添加“0”:

function AddDays(days) {

        var dateObject = new Date();
        dateObject.setDate(new Date().getDate() + days);
        var year = dateObject.getFullYear();
        var month = dateObject.getMonth()+1 < 10 ? "0" + (dateObject.getMonth()+1) : dateObject.getMonth()+1;
        var date = dateObject.getDate() < 10 ? "0" + dateObject.getDate() : dateObject.getDate();
        return year + "-" + month + "-" + date;

}

alert(AddDays(365));
于 2013-08-07T06:55:47.333 回答
1
var today = new Date();
console.log(today.getFullYear()+'-'+((today.getMonth()+1>9)?today.getMonth():'0'+(today.getMonth()+1))+'-'+(today.getDate()<10?'0'+today.getDate():today.getDate()));

小提琴:- http://jsfiddle.net/Ge4cw/

之前也有人问过:-

从 JS 日期对象获取 YYYYMMDD 格式的字符串?

于 2013-08-07T07:00:05.457 回答
0
function AddDays(days) {

            var thisDate = new Date();
            thisDate.setDate(thisDate.getDate() + days);
            return thisDate;

        }

alert(getReadableDate(AddDays(365)));


function getReadableDate(date) {
        return [date.getFullYear(), fillZeroes(date.getMonth() + 1), fillZeroes(date.getDate())].join('-');

        function fillZeroes(text) {
            return (new Array(2 - (text || '').toString().length + 1)).join('0') + (text || '');
        }
    }
于 2013-08-07T06:50:20.653 回答
0

我曾经遇到过这个问题。我的解决方案是通过一个在必要时添加零的函数:

function addZero(date)
{
    if (date < 10)
       return "0"+date;
   return date;
}

或更小:

function addZero(date)
{
    return (date < 10)? "0"+date : date;
}
于 2013-08-07T06:51:29.817 回答
0

在哪里可以找到有关在 JavaScript 中格式化日期的文档?

我认为这个答案应该对你有所帮助。

thisDate.getMonth() 和 thisDate.getDate() 返回一个整数,可能只有一位。您应该将其转换为字符串以添加“0”:

例子

var m = thisDate.getMonth()+1; // One digit month
m = '' + (m<=9 ? '0' + m : m); // Two digit month
于 2013-08-07T06:54:24.187 回答
0

这每次都有效:

var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear() + "-" + (month) + "-" + (day);
alert(today);
于 2013-11-03T17:58:35.663 回答
-1

你需要这个:

function AddDays(days) {

            var thisDate = new Date();
            thisDate.setDate(thisDate.getDate() + days);
            var dd = ( thisDate.getFullYear() + '-' + ("0"+ (thisDate.getMonth() + 1)).slice(-2) + '-' + ( "0" + thisDate.getDate()).slice(-2) );
            return dd.toString('yyyy-MM-DD');;
        }
        alert(AddDays(365));

http://jsfiddle.net/w9fxr/1/

再次检查我亲爱的朋友,它将处理所有月份的单位数和双位数

于 2013-08-07T06:52:43.177 回答
-1

只为一个想法,解释清楚

http://blog.justin.kelly.org.au/simple-javascript-function-to-format-the-date-as-yyyy-mm-dd/

于 2013-08-07T06:52:57.853 回答
-1

尝试这个:

var thisDate = new Date();

alert(thisDate.getFullYear() + '-' + ("0" + (thisDate.getMonth() + 1)).slice(-2) + '-' + ("0" + (thisDate.getDate() + 1)).slice(-2));

小提琴

于 2013-08-07T06:54:30.453 回答