Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date to milliseconds by javascript? [duplicate]

I have multiple date's for example(25-12-2017) i need them to be converted to milliseconds by javascript

like image 692
Ashok Kumar Avatar asked Mar 13 '18 04:03

Ashok Kumar


People also ask

How do you convert Date to milliseconds?

This is the number of seconds since the 1970 epoch. To convert seconds to milliseconds, you need to multiply the number of seconds by 1000. To convert a Date to milliseconds, you could just call timeIntervalSince1970 and multiply it by 1000 every time.

Is JavaScript Date now in milliseconds?

Date. now() returns the number of milliseconds since January 1, 1970.

Is JavaScript timestamp in milliseconds?

getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.


1 Answers

One way is to use year, month and day as parameters on new Date

new Date(year, month [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

You can prepare your date string by using a function.

Note: Month is 0-11, that is why m-1

Here is a snippet:

function prepareDate(d) {
  [d, m, y] = d.split("-"); //Split the string
  return [y, m - 1, d]; //Return as an array with y,m,d sequence
}

let str = "25-12-2017";
let d = new Date(...prepareDate(str));

console.log(d.getTime());

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

like image 177
Eddie Avatar answered Sep 27 '22 20:09

Eddie