-5

I have a date as string like this:

2018-05-29-13-56-00

I need to convert this to epoch so I can make calculations with other dates.

Does anyone have an idea on how to do that? Thank you!

C. Ubkcah
  • 273
  • 13
  • 33
  • Split the string by `-`, extract the numbers, and create a date object from it, then call `getTime()` on the date object. – 31piy May 29 '18 at 12:01
  • Date library moment.js will help you do both convetrting string to date object and do the calculations – charlietfl May 29 '18 at 12:05

2 Answers2

1

You can do this. Note the d[1] - 1 because months are zero-based.

const date = '2018-05-29-13-56-00';
const d = date.split('-');
const epoch = (new Date(d[0], d[1] - 1, d[2], d[3], d[4], d[5])).valueOf();
console.log(epoch);
Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
0

You can do this with moment.js, if you are okay with using that library.

moment('2018-05-29-13-56-00', 'YYYY-MM-DD-hh-mm-ss').format();