0

I am a newbie to javascript, and what I am trying to do is this: The webpage prompts you with "how many nights will you be staying?" you put in a number and it is supposed to look something like this "Total cost for checking in [today date] and checking our [nights staying] cost $"

I can't get the date to display the [nights stayed] if just gives random dates not x days from today. here is an example of what is displayed if i enter 5 in the prompt "Total cost for checking in Thu Sep 24 2020 and checking out Wed Jan 01 0245 is $500" not even a real year, nor is it 5 days from today.

var totalnights = prompt('How many nights will you be staying?');

var today;
var costMsg;
var nights;

function TotalCost(today) {
   
nights = new Date(new Date().getDate()+(totalnights));
    nights = (nights.toDateString());
    

today = new Date()
    today = (today.toDateString());


var Msg = 'Total cost for checking in ' + today + ' and checking out ' + nights + ' is ' + '$' + hotel.roomRate * totalnights ;
return Msg;
}
  • 1
    `new Date(new Date().getDate()+(totalnights));` you're adding `totalNights` to the date of month ... so you're creating a date from a number less than 50 ... so your date is 1 january 1970 - that's not anywhere near the code I suggested in your previous question - try https://pastebin.com/dtFGQ4fp – Jaromanda X Sep 25 '20 at 04:06

1 Answers1

0

The place you are going wrong is the date calculation. new Date().getDate() just gives back the current and the expected input parameter for the Date are given in the link.

You can fix the above code by

let night = new Date()
night.setDate(night.getDate() + totalnights)
deepak thomas
  • 1,118
  • 9
  • 23