0

I know there are many questions relative to this, but I can't find exactly what I am looking for..

I am creating an iOS Rideshare app and am utilizing the Google Distance Matrix. What I am looking to do is taking the current date and set it to midnight. For example: currentDate = 12/6/2018 12:00:00.

I want to take this currentDate value and convert is to Epoch and set it up as a baseEpoch value. This way, I can take the user's time input, and add the difference to get the date/time they entered in Epoch form.

I've tried solutions such as:

function convertToEpoch(time)
{
  var sep = time.split(':');
  var seconds = (+sep[0]) * 60 * 60 + (+sep[1]) * 60 + (+sep[2]);
  return seconds;
}

function currentDateAsEpoch(time) {
  var time = new Date();
  time.setHours(0,0,0,0);

  convertToEpoch(time);
}

const baseEpoch = currentDateAsEpoch();

But am getting the error: TypeError: time.split is not a function

I want the baseEpoch to be set as the current date so Google Distance Matrix doesn't return the departure_time error saying time can only be equal to or in the future.

Thank you in advance for your help!

LameBanana
  • 49
  • 4

1 Answers1

0

You can use momentjs package for nodejs. This package helps you in dealing date and time effortlessly. You may need to dig more into the docs for better understanding of the moment module(Documentation is simple to understand). Here is some snippet from momentjs docs.

moment().unix();
//moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).

moment(1318874398806).unix(); // 1318874398

Also using a library like moment which is well tested is better than writing your own functions to handle date and time for following reasons.

  • The code would be very well tested due to large number of people using them in a day to day basis.
  • No need to waste your time in reinventing the wheel.
  • Additional functionalities for future development work.(Like formatting and other date operations)
  • Easy to understand and implement even for new team members (due very well written documents and good support due to large number of developers using the library)
Shiva
  • 543
  • 1
  • 6
  • 20