-3

functionality:

User to play a game and when user wins, they will be navigated to a redemption page. Users are to redeem the goodies from the goodie page within 72hrs. Therefore, the redemption will expire 72 hours from the system time that the user is at the "congratulation" redemption page.

what has been done:

First, I have tried to get the current systemTime the moment user is navigated to the congratulation page.

Secondly, I have tried to get the expiry date by adding 3 additional days to set it as 72 hours later.

Lastly, I try to append the calculated expiry date to the <div> for expiry date.

Issue:

Somehow, the expiry date is not shown correctly. For example, if today's date is the 8/3, however, the result is showing 14/2.

Therefore, the date is not showing 72 hours later but a month and a few days ago.

What have I done wrong??

 function win() {

   //get current System Time and Date
   var endDate = new Date();

   //Get Expiry Date
   var date = new Date(endDate);
   date.setDate(date.getDate() + 3);
   var expiryDate = (date.getDate() + 3) + "/" + (date.getMonth());
   document.getElementById('ExpiryDate').innerHTML = expiryDate;

   console.log("test");
   $("#my-memory-game").fadeOut(function() {
     win_video();
   })
 }
<div id="congratulations">
  <canvas id="MyCanvas" style="width:1080px; height: 1920px;">
    Your browser does not support the HTML5 canvas tag.
  </canvas>
  <div id="ExpiryDate"></div>
</div>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Luke
  • 982
  • 1
  • 7
  • 27
  • [`date.getMonth()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth) is 0 index based so `2` is March – Hacketo Mar 08 '16 at 08:39
  • @Hacketo so I have to do a +2 at getMonth()?? – Luke Mar 08 '16 at 08:39
  • No, If 2 is march you should do +1 to get 3 ... . But there is another issue, if day is 31 you'll add 3 day to 31 that make 34, day 34 does not exists .. Look this thread : http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date – Hacketo Mar 08 '16 at 08:43
  • 1
    No you should do a +3 since march is 3. So the value you get from getMonth() +3 is the correct value. – Mike Lammers Mar 08 '16 at 08:44
  • @MikeLammers why `getMonth() +3` ? makes no sense .. would return 5 – Hacketo Mar 08 '16 at 08:48
  • Yea you are right, It does not make no sense. Sorry – Mike Lammers Mar 08 '16 at 08:51
  • @Hacketo, Right now I am getting numeric as month. Therefore, if I would like to get the character of each individual months. the steps that I would need to take would be 1.) set an array of month list = var MonthList["..",".."] then 2.) for e.g: MonthList[date.getMonth()]?? correct? – Luke Mar 08 '16 at 08:57
  • @Luke yes, as getMonth is 0 index based, you can easily use an array – Hacketo Mar 08 '16 at 09:01
  • @Hacketo Thanks. Got It! – Luke Mar 08 '16 at 09:02
  • You should not be doing this on the client at all. When the user wins, send a message to the sever and keep track of time there. – RobG Mar 08 '16 at 13:20

2 Answers2

2

First, get the month right

In javascript month indexes go from 0 to 11 and not from 1 to 12. So you should always do date.getMonth() + 1 when displaying the month numerical value. That is why you get 2 (February) instead of 3 (March) in your case.

See Javascript getDate() reference

Then, the day

You add 3 days twice (8 + 3x2 = 14 yey).

date.setDate(date.getDate() + 3);
var expiryDate = (date.getDate() + 3) + "/" + (date.getMonth());

Remove the second + 3 and you should be good to go.

Pierre C.
  • 1,426
  • 1
  • 11
  • 20
1

You just need to add 3 to the date:

var now = new Date();

// Copying the date is actually pointless, but hey...
var expireDate = new Date(+now);

// Add 3 days
expireDate.setDate(expireDate.getDate() + 3);

// Show date and month, note that the month is zero indexed so add 1
// when displaying it
document.write(expireDate.getDate() + '/' + (expireDate.getMonth() + 1));

You can safely add days beyond the end of the month, 31 March plus 3 days will result in 3 April.

But it doesn't seem safe to do this on the client anyway as you don't know how accurate the host system clock is. I'd send a message to the server and keep track of things there.

RobG
  • 142,382
  • 31
  • 172
  • 209