Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date Plus 2 Weeks (14 days)

I use this to get the date:

var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() alert(month + "/" + day + "/" + year); 

How can I add 2 weeks ? So instead of showing 10/13/2011, to show 10/27/2011 etc

Here is the fiddle: http://jsfiddle.net/25wNa/

I want the one input to have +14 days and the other +21

Note: I'd like the format to be > 10/13/2011 <.

like image 607
jQuerybeast Avatar asked Oct 13 '11 09:10

jQuerybeast


People also ask

How do you add weeks to a date in typescript?

function addWeeks(numOfWeeks, date = new Date()) { date. setDate(date. getDate() + numOfWeeks * 7); return date; } // Add 2 weeks to current Date console.

How do I calculate the date in JavaScript three months prior to today?

This is the Smallest and easiest code. var minDate = new Date(); minDate. setMonth(minDate. getMonth() - 3);


2 Answers

12096e5 is a magic number which is 14 days in milliseconds.

var fortnightAway = new Date(Date.now() + 12096e5); 

jsFiddle.

like image 140
alex Avatar answered Nov 04 '22 23:11

alex


var currentTime = new Date(); currentTime.setDate(currentTime.getDate()+14); 
like image 45
YuS Avatar answered Nov 05 '22 01:11

YuS