0

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();
4
  • why there is 14 in month of dates array? Commented Jan 3, 2020 at 6:29
  • 12 is the date value not month yy-dd-mm @Kaushik Commented Jan 3, 2020 at 6:30
  • do you expect an array of timestamps are your result? Commented Jan 3, 2020 at 6:31
  • yes. kind of @NickParsons. Just want to convert array of datetime into timestamp Commented Jan 3, 2020 at 6:32

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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);

2 Comments

does it also contains the microsecond value
yes, it takes that into consideration. As you can see, all your dates are exactly the same except for the microseconds. If it didn't contain the microseconds then all the timestamps would be the same, but they're different
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.