0

I am passing in time values departureTime such as '12:00' here. Then I try to convert it into a date and perform a subtraction. However, the result of timeOfDeparture and then time till departure is invalid. How can I fix this and make everything in a uniform type?

    const timeNow = new Date();
    console.log('TIME NOW', timeNow)
    const timeOfDeparture = new Date(departureTime);
    console.log('TIME of Departure', timeOfDeparture)
    const timeTillDeparture = Math.floor((timeOfDeparture.valueOf() - timeNow.valueOf()) / 60000);
    console.log('TIME TILL DEP', timeOfDeparture)

Example console logs:

TIME NOW Tue Oct 06 2020 15:47:35 GMT+0200 (Central European Summer Time)
TIME of Departure Invalid Date
TripTimes.tsx:17 TIME TILL DEP NaN
  • date string must be full date, not just hour. For example `const timeOfDeparture = new Date('October 6, 2020 12:00:00');` – firatozcevahir Oct 06 '20 at 14:00
  • where is the `departureTime` ? Is it undefined? – Sarun UK Oct 06 '20 at 14:00
  • Is it possible to get current date and add 1 hour to it? That would fix my problem of defining a future time value for now. @firatozcevahir –  Oct 06 '20 at 14:07
  • 1
    yes it is. `const myDate = new Date(); myDate.setHours(myDate.getHours() +1);` this will add 1 hour to current date. – firatozcevahir Oct 06 '20 at 14:13
  • Does this answer your question? [How to parse a time into a Date object from user input in JavaScript?](https://stackoverflow.com/questions/141348/how-to-parse-a-time-into-a-date-object-from-user-input-in-javascript) – Heretic Monkey Oct 06 '20 at 15:08
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Oct 06 '20 at 22:53

2 Answers2

0

The date object doesn't take hours as an argument. you can read more about it here

There are four ways of instantiating a date:

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

If your app relies on date manipulation, or if you want to manipulate dates more easily, consider using helper libraries like momentJs.

For example, if your departure time is on the same date but just on a different hour, your departure time using momentJs will be

moment().hour(departureHour).minute(departureMinute);
akram-adel
  • 920
  • 7
  • 8
  • Is it possible to get current date and add 1 hour to it? That would fix my problem of defining a future time value for now. –  Oct 06 '20 at 14:06
  • Yes, you can try `date.setHours()`. More about it [here](https://www.w3schools.com/jsref/jsref_sethours.asp) – akram-adel Oct 06 '20 at 14:09
  • No, sorry. `setHours()` will just set the hour to a defined value. If you want to add 1 hour to the current date you do `new Date(d.getTime() + (60 * 60 * 1000))`. where you simply add 1 hour (in milliseconds) to the current date – akram-adel Oct 06 '20 at 14:13
  • Please don't reference w3schools, [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) is much preferred. – RobG Oct 07 '20 at 22:51
0

When you are creating a Javascript Date object you cannot pass only hours and minutes. You can use the empty constructor or data constructor

new Date(year, month, date, hours, minutes, seconds, ms)

but the first two arguments (year and month) are obligatory at least. You could get data from timeNow and add the time values from departureTime. Example:

const timeNow = new Date();
console.log('TIME NOW', timeNow)
let departureTimeArray = departureTime.split(":");
console.log('Hour of Departure', departureTimeArray[0]);
console.log('Minutes of Departure', departureTimeArray[1]);
const timeOfDeparture = new Date(timeNow.getFullYear(), timeNow.getMonth(), timeNow.getDate(), departureTimeArray[0], departureTimeArray[1]);
console.log('TIME of Departure', timeOfDeparture);
const timeTillDeparture = Math.floor((timeOfDeparture.valueOf() - timeNow.valueOf()) / 60000);
console.log('TIME TILL DEP', timeOfDeparture)

I hope this solution helps you.

Víctor
  • 493
  • 5
  • 7