Javascript date formatting

孤街浪徒 提交于 2019-12-13 09:46:59

问题


I've tried my level best but couldn't get the result. What I need is the date in YYYY-MM-DD format. Here is my current code:

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));

I'm getting "2014-8-7" as result but I want to get it as YYYY-MM-DD. I need it to look like "2014-08-07".


回答1:


You need to check if the date or month is under 10, if it is, add "0" before the date/month:

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));



回答2:


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()));

Fiddle for this:- http://jsfiddle.net/Ge4cw/

It has also been previously asked:-

Get String in YYYYMMDD format from JS date object?




回答3:


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 || '');
        }
    }



回答4:


I faced that problem once. My solution was to pass by a function who add zero if necessary:

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

or smaller :

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



回答5:


Where can I find documentation on formatting a date in JavaScript?

I think this answer should help you.

thisDate.getMonth() and thisDate.getDate() return an integer, which may have only one digit. You should transform it to string to add the "0":

Example

var m = thisDate.getMonth()+1; // One digit month
m = '' + (m<=9 ? '0' + m : m); // Two digit month



回答6:


This works everytime:

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);



回答7:


Just for an Idea, Explained clearly

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




回答8:


Try this:

var thisDate = new Date();

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

Fiddle




回答9:


you need this:

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/

check again my dear friend it will handle all month single and double digit all



来源:https://stackoverflow.com/questions/18096442/javascript-date-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!