Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  12] [ 1]  / answers: 1 / hits: 153181  / 11 Years ago, mon, november 11, 2013, 12:00:00

I am calculating 12 days before date from today date. But it does not return the correct date.
For example, for today dat, 11/11/2013 in (mm/dd/yyyy), it returns 10/30/2013 when it should return 10/31/2013.



Here is the code



var d = new Date();
d.setDate(d.getDate() - 12);
d.setMonth(d.getMonth() + 1 - 0);
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
if (curr_month < 10 && curr_date < 10) {
var parsedDate = 0 + curr_month + / + 0 + curr_date + / + curr_year;
alert(parsedDate);
} else if (curr_month < 10 && curr_date > 9) {
var parsedDate = 0 + curr_month + / + curr_date + / + curr_year;
alert(parsedDate);
} else if (curr_month > 9 && curr_date < 10) {
var parsedDate = curr_month + / + 0 + curr_date + / + curr_year;
alert(parsedDate);
} else {
var parsedDate = curr_month + / + curr_date + / + curr_year;
alert(parsedDate);
}

More From » date

 Answers
18

Problem is solved



var days; // Days you want to subtract
var date = new Date();
var last = new Date(date.getTime() - (days * 24 * 60 * 60 * 1000));
var day =last.getDate();
var month=last.getMonth()+1;
var year=last.getFullYear();

[#74354] Sunday, November 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braydon

Total Points: 0
Total Questions: 102
Total Answers: 111

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
;