0

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

AleMal
  • 1,977
  • 6
  • 24
  • 49
  • Hi, i don't need the difference between two date, i just want the date value resulting from todaydate-somenumberofdays – AleMal Aug 25 '14 at 07:13
  • What does stringToDate function do? You are passing a Date value into getDifDate and then it looks like you are trying to convert it from string to date with d = stringToDate(d); which you don't need to do – Martin Aug 25 '14 at 07:18

2 Answers2

2

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

Jyoti Puri
  • 1,326
  • 10
  • 17
1

Use DateJS library

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

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

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

michalh
  • 2,907
  • 22
  • 29