Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript date + 7 days

What's wrong with this script?

When I set my clock to say 29/04/2011 it adds 36/4/2011 in the week input! but the correct date should be 6/5/2011

var d = new Date(); var curr_date = d.getDate(); var tomo_date = d.getDate()+1; var seven_date = d.getDate()+7; var curr_month = d.getMonth(); curr_month++; var curr_year = d.getFullYear(); var tomorrowsDate =(tomo_date + "/" + curr_month + "/" + curr_year); var weekDate =(seven_date + "/" + curr_month + "/" + curr_year); { jQuery("input[id*='tomorrow']").val(tomorrowsDate); jQuery("input[id*='week']").val(weekDate);     } 
like image 771
user472285 Avatar asked Apr 21 '11 08:04

user472285


People also ask

How do you add 7 days to a date?

Just do: $date = strtotime("+7 day"); echo date('M d, Y', $date);

How do you get the day of the week from a date in JavaScript?

getDay() The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.


2 Answers

var date = new Date();  date.setDate(date.getDate() + 7);    console.log(date);

And yes, this also works if date.getDate() + 7 is greater than the last day of the month. See MDN for more information.

like image 161
adam77 Avatar answered Oct 14 '22 14:10

adam77


Without declaration

To return timestamp

new Date().setDate(new Date().getDate() + 7) 

To return date

new Date(new Date().setDate(new Date().getDate() + 7)) 
like image 44
Emanuel Avatar answered Oct 14 '22 14:10

Emanuel