52

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 <.

alex
  • 479,566
  • 201
  • 878
  • 984
jQuerybeast
  • 14,130
  • 38
  • 118
  • 196

9 Answers9

120

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

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

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    @jQuerybeast: It doesn't for me. – alex Oct 13 '11 at 09:31
  • Ah my mistake. I want it on my format that's why I asked the question. Can you check my fiddle and the edit? Thanks – jQuerybeast Oct 13 '11 at 09:32
  • @jQuerybeast Simply use your existing code, but replace `currentTime` with `fortnightAway`. – alex Oct 13 '11 at 09:51
  • Where have you got the 12096e5 from? Like in case I want to add more days etc. – jQuerybeast Oct 13 '11 at 10:01
  • 6
    @jQuerybeast: `1000 * 60 * 60 * 24 * 14`, *milliseconds in one second * seconds in a minute * minutes in an hour * hours in a day * days in 2 weeks*. – alex Oct 13 '11 at 10:22
  • 1
    Have you noticed that if you add more than 14 days, the month doesn't change? – jQuerybeast Oct 13 '11 at 17:52
  • @jQuerybeast I doubt that, do you have a fiddle? – alex Oct 13 '11 at 22:05
  • @jQuerybeast You are adding a month to `a` and using that when in both inputs. That's why it's the same month - it's the same variable. – alex Oct 14 '11 at 00:19
  • @alex Is this really robust, though? Like when a day is not strictly 24 hours, like happened recently? – julien_c Aug 01 '12 at 14:34
  • A day isn't strictly 24 hours, but I believe it's safe to measure them at 24 hours. – alex Aug 01 '12 at 23:24
  • As commented below: if you want to handle DST -> function addDays(noOfDays, date) { var origTimezoneOffset = date.getTimezoneOffset(); date = new Date(date.getTime() + (noOfDays * (1000 * 60 * 60 * 24))); var offsetDiff = (date.getTimezoneOffset() - origTimezoneOffset) * 60 * 1000; date = new Date(date.getTime() + offsetDiff); return date; } – Justin Pihony Aug 28 '14 at 18:25
  • What if want to increase number of days?? – Tayyab Dec 10 '21 at 15:34
55
var currentTime = new Date();
currentTime.setDate(currentTime.getDate()+14);
YuS
  • 2,025
  • 1
  • 15
  • 24
  • Can you please check my edit? Id like it in a format of DD/MM/YY. That's why I asked my question because if I add on that format it bypasses the maximum days of a month. I get a value of 34/10/2011. Thanks – jQuerybeast Oct 13 '11 at 09:56
  • Just put `currentTime.setDate(currentTime.getDate()+14);` before you defining separate day, month and year variables in your function. It should works. – YuS Oct 13 '11 at 10:07
  • Does this work if adding 14 days would put you in the next month? – Nathan Arthur Jan 22 '20 at 12:16
  • 1
    @NathanArthur Surprisingly, it does work for any number of months/years after/ago – Arthur Khazbs Sep 14 '22 at 09:58
11

12096e5 is a kind of magic number. Just 14 days in milliseconds in exponential notation.

This number is the result of 1000[ms] * 60[s] * 60[m] * 24[h] * 14[d] saved in exponential notation.

You can check it if you type Number('12096e5'). You will get 1209600000 [ms] which is exactly 2 weeks in milliseconds. The exponential notation makes it obscure.

You can write any other number in exponential notation to make the life of your fellow developers more interesting.

Date object has constructor which accepts milliseconds as an argument which argument can be in exponential notation.

var d = new Date(milliseconds);

var afterTwoWeeks = new Date(+new Date + 12096e5);
var afterTwoWeeks = new Date(+new Date + 1209600000);

Both are the same.

Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70
  • 4
    *Magic number* [Wikipedia](https://en.wikipedia.org/wiki/Magic_number_(programming)) – alex Mar 05 '18 at 11:50
  • 1
    Note that exponential notation is not a real-world example in the Wikipedia reference to magic numbers nor is it an example in a protocol, since it is one number defined in exponential notation as Ognyan pointed out – aug2uag Aug 22 '18 at 03:38
  • @alex I agree that to a large extent this is a magic number/string. It is magic 'literal' but since the confusion comes from the type of notation I wrote that it is not a magic literal which was not correct. – Ognyan Dimitrov Aug 22 '18 at 06:34
11

have made a fidle for you http://jsfiddle.net/pramodpv/wfwuZ/

    Date.prototype.AddDays = function(noOfDays) {
       this.setTime(this.getTime() + (noOfDays * (1000 * 60 * 60 * 24)));
       return this;
    }

    Date.prototype.toString = function() {
       return this.getMonth() + "/" + this.getDate() + "/" +  this.getFullYear().toString().slice(2); 
    }

    $(function() {
        var currentTime = new Date();
        alert(currentTime.AddDays(14));
    });
  • Can you please check my edit? Id like it in a format of DD/MM/YY. That's why I asked my question because if I add on that format it bypasses the maximum days of a month. I get a value of 34/10/2011. Thanks – jQuerybeast Oct 13 '11 at 09:55
  • if you are using the DD/MM/YY throughout you can override the dates toString function. i have edited my answer and fiddle as per your date format. js fiddle link is http://jsfiddle.net/pramodpv/wfwuZ/2/ – Pramod Pallath Vasudevan Oct 14 '11 at 01:29
  • 1
    I liked the AddDays function, however it does not handle dst switches if you want that: So, I made it more like this: function addDays(noOfDays, date) { var origTimezoneOffset = date.getTimezoneOffset(); date = new Date(date.getTime() + (noOfDays * (1000 * 60 * 60 * 24))); var offsetDiff = (date.getTimezoneOffset() - origTimezoneOffset) * 60 * 1000; date = new Date(date.getTime() + offsetDiff); return date; } – Justin Pihony Aug 28 '14 at 16:20
  • Overwriting `Date.prototype.toString()` is probably a bad idea – alex Mar 05 '18 at 11:49
11

Try this:

currentTime.setDate(currentTime.getDate()+14);
uvesten
  • 3,365
  • 2
  • 27
  • 40
刘伟科
  • 432
  • 3
  • 12
5

Well, JS times are in millisecond, so adding two weeks would be a case of working out what two weeks is in milliseconds, and adding that value.

var twoWeeks = 1000 * 60 * 60 * 24 * 14;
var twoWeeksTime = new Date(new Date().getTime() + twoWeeks);
var formattedDate = twoWeeksTime.getDate() + '/' + (twoWeeksTime.getMonth()+1) + '/' + twoWeeksTime.getYear();

Of course, this method falls down if you need to add months, since they're variable in length, but it's fine for adding days and weeks.

Alternatively, you use the DateJS library, which has functionality for exactly this kind of thing (plus loads more).

With DateJS, your code could look like this:

var twoWeeksTime = Date.today().add({ days: 14 });
var formattedDate = twoWeeks.TimetoString('dd/MM/yy');

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • I think `var twoWeeks = 1000* 60 * 60 * 24 * 14;` – Narendra Yadala Oct 13 '11 at 09:30
  • Can you please check my edit? Id like it in a format of DD/MM/YY. That's why I asked my question because if I add on that format it bypasses the maximum days of a month. I get a value of 34/10/2011. Thanks – jQuerybeast Oct 13 '11 at 09:36
  • @jQuerybeast - using the technique from your the question with the `twoWeeksTime` variable in my answer will give you a valid date. Have you tried it? But anyway, I've added a formatting line of code to my answer, plus a bit of code to demo how the DateJS makes things easier. – Spudley Oct 13 '11 at 09:49
  • @jQuerybeast - fair enough; I've given you an equally good bit of code that doesn't use a plugin. – Spudley Oct 13 '11 at 09:54
  • @jQuerybeast - ironically, your JSFiddle goes to an error 404. But yes, I have tested it... and you're right, I had forgotten to add the `+1` to the month, but since you already had that in the code in your question, I guess you're smart enough to have worked out where I went wrong. (I've now added the `+1` though) – Spudley Oct 13 '11 at 10:00
  • @spudley +1 on the month makes no difference if it works or not. Since it wasn't compiling I couldn't check if its 1 month behind to change that. It is just not compiling. http://jsfiddle.net/FQzbz/ I am not THAT good at JavaScript and that's probably why I am asking this question in the first place? – jQuerybeast Oct 13 '11 at 17:51
  • well, I don't know what you're doing different to me, but I can paste those three lines of code into Firebug followed by `alert(formattedDate);`, and it gives me the correct date for two weeks time. Here's a working JSFiddle: http://jsfiddle.net/KXMEx/ – Spudley Oct 14 '11 at 07:50
2

Add or Subtract 2 weeks from current date

Below code example give output in YYYY-MM-DD format

If condition is added in the string to concatenate 0 with Month and Day which is less than 10.

var twoWeeks = 1000 * 60 * 60 * 24 * 14;
var twoWeeksTime = new Date(new Date().getTime() + twoWeeks); /* Add or Subtract here*/

var formattedDate = (twoWeeksTime.getFullYear()) + '-' +
                    ((twoWeeksTime.getMonth()+1) < 10 ? "0"+(twoWeeksTime.getMonth()+1): (twoWeeksTime.getMonth()+1)) + '-' +
                    (twoWeeksTime.getDate() < 10 ? "0"+(twoWeeksTime.getDate()): (twoWeeksTime.getDate()));

document.body.innerHTML = formattedDate;
Shirjeel Ahmed Khan
  • 257
  • 1
  • 5
  • 12
0

add the following prototype method

Date.prototype.addDays = function(days) {
     this.setDate(this.getDate()+days);
}

and than its very simple to use,

currentTime.addDays(5);
Shlomi Komemi
  • 5,445
  • 3
  • 28
  • 41
-1

If you are formatting a javascript date in a particular format, then I think you can have a look at this script http://blog.stevenlevithan.com/archives/date-time-format. All you would need to do after including the script is this new Date(+new Date + 1000* 60 * 60 * 24 * 14).format('dd/mm/yyyy') and you would get the output "27/10/2011"

The script is pretty small, just above 1KB minified. This is the link to a working fiddle http://jsfiddle.net/naryad/GufvT/

Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43