问题描述:

i need a date in "YYYY-mm-dd" format from some days past.

i write this function:

function getDifDate(d, numd) {

d = stringToDate(d);

d.setDate(d.getDate() - numd);

return d;

}

then in another part of the program i write:

var tod = new Date();

switch(selPer.value) {

case 1:

x= getDifDate(tod, 2);

break;

case 2:

x= getDifDate(tod, 15);

break;

default:

//default code block

}

console.log("Data1: "+ x);

but the x value, for example if i launch today my script return ever "25"

What is wrong?

Thanks in advance

网友答案:

This should work fine even without DateJS

function getDifDate(d, numd) {
 d.setDate(d.getDate() - numd);
 return d;
}

var x;
var tod = new Date();
switch(1) {
            case 1:         
                x= getDifDate(tod, 2);
                break;
            case 2:
                x= getDifDate(tod, 15);
                break;
            default:
                //default code block
        }

console.log("Data1: ", x);

Check this plunker: http://plnkr.co/edit/gLwBIfAp7zFLEaV1KJ1O?p=preview

网友答案:

Use DateJS library

    Date.today().addDays(-1);

Documentation: https://code.google.com/p/datejs/wiki/APIDocumentation

A working jsFiddle: http://jsfiddle.net/vx1m67w1/1/

相关阅读:
Top