saheb11 / datejs

Automatically exported from code.google.com/p/datejs
Other
0 stars 0 forks source link

Add a year (365 or 366 days) to de current day #146

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
var start = Date.today();
var end = Date.parse(start, "d-MMM-yyyy");
end.addYears(1); //one year later
console.log(end);

What is the expected output? What do you see instead?
the difference between 'start' and 'end' should be 365 or 366 days, depending 
of the year, leap year or not.

What version of the product are you using? On what operating system?
version: Nov 2007 

Please provide any additional information below.

Should I add days manually? Why is not working addYears(1) ??

Thanks in advance

Original issue reported on code.google.com by jose.ros...@gmail.com on 27 Feb 2012 at 12:13

GoogleCodeExporter commented 8 years ago
Example of what I need: 

start date: 01 Mar 2012
  end date: 28 Feb 2013

thanks.

Original comment by jose.ros...@gmail.com on 27 Feb 2012 at 12:30

GoogleCodeExporter commented 8 years ago
I've got the solution:

var start = Date.today();
var end = Date.parse(start, "d-MMM-yyyy");

if (end.toString('d/M') != '29/02') {
  end.addDays(-1); //in case that it isn't 29 of Feb, we remove a day
}
end.addYears(1); 

console.log(end);

thanks.

Original comment by jose.ros...@gmail.com on 27 Feb 2012 at 12:54

GoogleCodeExporter commented 8 years ago
It's because you're parsing the Start var - it returns the object so the end 
var function ends up mutating the original object (which end ends up as a 
reference to).

var start = Date.today();
var end = Date.today();
end.addYears(1); 

That works as expected, no mutation of the object. Maybe it should clone it...?

Original comment by darkcr...@gmail.com on 16 Sep 2013 at 4:33

GoogleCodeExporter commented 8 years ago
patched it so it clones the object if it's an instance of a date object in my 
fork: 
https://github.com/abritinthebay/datejs/

Original comment by darkcr...@gmail.com on 16 Sep 2013 at 4:45