65

Javascript definitely isn't my strongest point. I've been attempting this for a couple of hours now and seem to be getting stuck with date formatting somewhere.

I have a form where a user selected a date (dd/mm/yyyy) and then this date will be taken and 2 weeks will be added to it and then date will be copied to another form field.

My latest attempt below isn't even adding a date yet just copying the selected date in one form field to another, if I select '03/02/2012', it outputs 'Fri Mar 02 2012 00:00:00 GMT+0000 (GMT Standard Time)', so its outputting in American format as well as the full date. How to I get it to out put in the same format and add 2 weeks?

function LicenceToOccupy(acceptCompletionDate)
{
    var date1 = new Date(acceptCompletionDate);
    document.frmAccept.acceptLicence.value = date1;

}
Coral Doe
  • 1,925
  • 3
  • 19
  • 36
Jammer
  • 2,330
  • 11
  • 48
  • 77

6 Answers6

152

You can do this :

const numWeeks = 2;
const now = new Date();
now.setDate(now.getDate() + numWeeks * 7);

or as a function

const addWeeksToDate = (dateObj,numberOfWeeks) => {
  dateObj.setDate(dateObj.getDate()+ numberOfWeeks * 7);
  return dateObj;
}

const numberOfWeeks = 2 
console.log(addWeeksToDate(new Date(), 2).toISOString());

You can see the fiddle here.

According to the documentation in MDN

The setDate() method sets the day of the Date object relative to the beginning of the currently set month.

Arvind Sridharan
  • 3,885
  • 4
  • 29
  • 54
12

This might not answer the question per se, but one can find a solution with these formulas.

6.048e+8 = 1 week in milliseconds

Date.now() = Now in milliseconds

Date.now() + 6.048e+8 = 1 week from today

Date.now() + (6.048e+8 * 2) = 2 weeks from today

new Date( Date.now() + (6.048e+8 * 2) ) = Date Object for 2 weeks from today

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
Luis
  • 789
  • 9
  • 9
  • One week in milliseconds is NOT `6.04e+8` it is `6.048e+8`. Also be wary of DST if using this approach. – mtkopone May 21 '20 at 12:35
7

You're assigning date1 to be a Date object which represents the string you pass it. What you're seeing in the acceptLicense value is the toString() representation of the date object (try alert(date1.toString()) to see this).

To output as you want, you'll have to use string concatenation and the various Date methods.

var formattedDate = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();

In terms of adding 2 weeks, you should add 14 days to the current date;

date1.setDate(date.getDate() + 14);

... this will automatically handle the month increase etc.

In the end, you'll end up with;

var date1 = new Date(acceptCompletionDate);
date1.setDate(date1.getDate() + 14);
document.frmAccept.acceptLicence.value = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();

N.B Months in JavaScript are 0-indexed (Jan = 0, Dec = 11), hence the +1 on the month.

Edit: To address your comment, you should construct date as follows instead, as the Date argument is supposed to be "A string representing an RFC2822 or ISO 8601 date." (see here).

var segments = acceptCompletionDate.split("/");
var date1 = new Date(segments[2], segments[1], segments[0]);
Matt
  • 74,352
  • 26
  • 153
  • 180
  • Thanks for your response, if I select '04/01/2012' (dd/mm/yyyy) the value i get in the other field is '15/4/2012' (dd/mm/yyyy) so it seems to still be adding 14 to the month rather than the day, or more likely the formatting is still wrong somewhere. – Jammer Jul 05 '12 at 12:21
  • I guess it needs formatting somehow before the 14 days are added? as the (date1.getDate() + 14) is assuming its formatted as mm/dd/yyyy – Jammer Jul 05 '12 at 12:24
  • @JBoom: `Date` argument is supposed to be "A string representing an RFC2822 or ISO 8601 date." ([see here](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse)). It might be better to use the other constructor which accepts year, month, day as separate parameters (see my edited answer). – Matt Jul 05 '12 at 12:25
  • Thanks Matt, format is fine but now the adding 1 to the month is messing it up, even if it's adding one or not! – Jammer Jul 05 '12 at 14:13
0

This should do what you're looking for.

function LicenceToOccupy(acceptCompletionDate)
{
    var date1 = new Date(acceptCompletionDate);
    date1.setDate(date1.getDate() + 14);
    document.frmAccept.acceptLicence.value = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();
}
Toast
  • 424
  • 2
  • 9
0

To parse the specific dd/mm/yyyy format and increment days with 14 , you can do something like split the parts, and create the date object with y/m/d given specfically. (incrementing the days right away) Providing the separator is always -, the following should work:

function LicenceToOccupy(acceptCompletionDate)
{
    var parts = acceptCompletionDate.split("/");
    var date1 = new Date(parts[2], (parts[1] - 1), parseInt(parts[0]) + 14); //month 0 based, day: parse to int and increment 14 (2 weeks)
    document.frmAccept.acceptLicence.value = date1.toLocaleDateString(); //if the d/m/y format is the local string, otherwise some cusom formatting needs to be done

}
Me.Name
  • 12,259
  • 3
  • 31
  • 48
-1
 date1.toLocaleDateString() 

Thiswill return you date1 as a String in the client convention

To create a new date date2 with 2 weeks more (2weeks = 27246060 seconds):

 var date2 = new Date(date1 + 60*60*24*7*2);
miken32
  • 42,008
  • 16
  • 111
  • 154
Ricola3D
  • 2,402
  • 17
  • 16
  • That's what [`toLocaleDateString`](http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.6) **should** do, but it doesn't in all browsers (e.g. Chrome ignores my system settings, as do recent versions, but not the current version, of Firefox). – RobG Oct 30 '12 at 05:34