获取日期数组的时间戳数值

huangapple go评论67阅读模式
英文:

Get TimeStamp value of Array of dates

问题

我有一个包含日期时间值(包括微秒)的日期数组。

所以我的问题是,如何将这些值转换为时间戳值,例如整数或浮点数,格式如下:

1578032412798

这里我有以下日期数组:

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"];

我尝试了以下代码,但它不起作用:

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

我还尝试了实现以下方法,但它只显示到小时:

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

希望这些信息对你有所帮助。

英文:

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

答案1

得分: 1

以下是翻译好的部分:

你可以使用正则表达式提取日期的年、月和日部分,然后使用它们按正确的顺序创建一个新的 Date 对象:

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);
英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

答案2

得分: 0

您可以使用.map().split()来重新格式化您的日期。首先,使用空格来.split()日期,然后再使用-.split()日期组件。然后,您可以交换日期中的日和月,并将其合并成日期时间字符串。使用new Date(),您可以从重新格式化的日期字符串中获取时间戳:

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

不含代码部分的翻译如上。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

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

<!-- end snippet -->

答案3

得分: 0

请避免使用var关键字。请使用constlet代替。

以下的代码应该适用于你的用例,至少如果你提供的所有日期都遵循相同的格式(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();
});

这不如使用正则表达式那么花哨,但更容易阅读。

英文:

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 =&gt; {
  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.

答案4

得分: 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.

huangapple
  • 本文由 发表于 2020年1月3日 14:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定