-1

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".

Maple
  • 741
  • 13
  • 28

9 Answers9

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));
Tommy Østgaard
  • 159
  • 1
  • 4
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()));

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

It has also been previously asked:-

Get String in YYYYMMDD format from JS date object?

Community
  • 1
  • 1
abhinsit
  • 3,214
  • 4
  • 21
  • 26
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 || '');
        }
    }
Dennis
  • 14,210
  • 2
  • 34
  • 54
0

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;
}
Yabada
  • 1,728
  • 1
  • 15
  • 36
0

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
Community
  • 1
  • 1
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
0

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);
roshan
  • 2,410
  • 2
  • 25
  • 37
-1

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

Notepad
  • 1,659
  • 1
  • 12
  • 14
-1

Just for an Idea, Explained clearly

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

shiva kumar
  • 74
  • 1
  • 5
-1

Try this:

var thisDate = new Date();

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

Fiddle

Abhishek Jain
  • 2,597
  • 1
  • 18
  • 12