0

I'm currently just practising JavaScript and I'm trying to create a programme that calculates how many days there are until your next birthday.

I have seen on this site that there is a daysBetween function that I can use to tell the time difference between two dates (I just need to turn these dates into the millisecond value since the 1st Jan 1970).

The problem is that, although one of those dates is the current date (which is easy to convert into its millisecond value), the second is derived from answers the user inputs as a string into a prompt command (there are three different boxes that ask for the year, month and date of their birth). Is there a way I can convert these input strings into a date format that I can then use to find the days between today's date and their next birthday?

Thanks and sorry if this is a stupid question!

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Molly
  • 1
  • 1
  • 1
    Possible duplicate of [javascript create date from year, month, day](https://stackoverflow.com/questions/40981982/javascript-create-date-from-year-month-day) – Peter Hall Jun 25 '17 at 14:14

1 Answers1

0

Remember in JS that months are indexed from 0, so January === 0.

var date = new Date('2017','5','25'); would be today, assume you have something like:

var userDd = '25';
var userMm = '6'; // User doesn't know the months are indexed at 0.
var userYy = '2017';

var date = new Date(userYy, userMm - 1, userYy);

date.getTime(); // returns the milliseconds value.
OneNeptune
  • 883
  • 11
  • 20