Chempooro
Chempooro

Reputation: 435

Get TimeStamp value of Array of dates

I have an array of dates that have the datetime values till microseconds .

So myquestion is that how can I convert those values to a Timestamp values such as in the form of integer or float no's as the following type:

1578032412798

Here I have the array of dates as following :

var dates =["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000"] ;

I am trying the following code. but it's not working :

 dates.forEach((e) => {
  var date = e.getTime();
  console.log (date)});

Also I tried to implement the following mwthod. but its showing the values till hours only:

var dateString = dates,
                     dateTimeParts = dateString.split(' '),
                     timeParts = dateTimeParts[1].split(':'),
                     dateParts = dateTimeParts[0].split('-'),
                   date;
                   x = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]);

mainval  = x.getTime();

Upvotes: 0

Views: 843

Answers (4)

HarshaHR
HarshaHR

Reputation: 73

Instead of storing dates as string in a array, store the Date object itself. If you push the Date object into a array, it will be very useful to extract milliseconds and all the functions related to Date object can be used.

Upvotes: 0

Nick
Nick

Reputation: 147166

You can use a regex to extract the year, month and day parts of the date and then create a new Date from a reconstructed string with them in the correct order:

var dates = [
  "2020-14-03 11:14:48.225000",
  "2020-14-03 11:14:48.225000", 
  "2020-14-03 11:14:48.226000",
  "2020-14-03 11:14:48.226000",
  "2020-14-03 11:14:48.227000", 
  "2020-14-03 11:14:48.227000",
  "2020-14-03 11:14:48.228000",
  "2020-14-03 11:14:48.228000",
  "2020-14-03 11:14:48.228000",
  "2020-14-03 11:14:48.229000",
  "2020-14-03 11:14:48.229000",
  "2020-14-03 11:14:48.229000",
  "2020-14-03 11:14:48.230000",
  "2020-14-03 11:14:48.230000",
  "2020-14-03 11:14:48.230000",
  "2020-14-03 11:14:48.231000",
  "2020-14-03 11:14:48.231000",
  "2020-14-03 11:14:48.231000",
  "2020-14-03 11:14:48.231000",
  "2020-14-03 11:14:48.232000"
];
res = dates.map(d => {
  m = d.match(/^(\d+)-(\d+)-(\d+) (.*)$/);
  return new Date(`${m[1]}-${m[3]}-${m[2]} ${m[4]}`).getTime();
});
console.log(res);

Upvotes: 1

Titulum
Titulum

Reputation: 11456

Please refrain from using the var keyword. Use const and let instead.

The following code should suit your usecase, at least if all the dates you provide follow the same format (yyyy-dd-mm hh:mm:ss:SSSS):

const timestamps = dates.map(e => {
  const year = e.substring(0, 4);
  const day = e.substring(5, 7);
  const month = e.substring(8, 10);
  const hours = e.substring(11, 13);
  const minutes = e.substring(14,16);
  const seconds = e.substring(17, 19);
  const milliseconds = e.substring(20);
  return new Date(year, month, day, hours, minutes, seconds, milliseconds).getTime();
});

It's not as fancy as using a regex, but it's a lot more readable.

Upvotes: 0

Nick Parsons
Nick Parsons

Reputation: 50724

You can use .map() with .split() to reformat your dates. First .split() by space to get the date, and then .split() the date by - to get the date components. You can then flip the day and month around and join that back into a datetime string. Using new Date(), you can then get the timestamp from the reformatted date string:

const dates =["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000"];

const res = dates.map(date_str => {
  const [date, rest] = date_str.split(' ');
  const [y, d, m] = date.split('-');
  return +new Date([[y, m, d].join('-'), rest].join(' '));
});
console.log(res);

Upvotes: 0

Related Questions