简体   繁体   English

用Javascript解析时间字符串

[英]Parsing time-strings in Javascript

So, I have this web server that parses an XML-file with a certain 'time-period'. 因此,我有这个Web服务器,它使用某个“时间段”解析XML文件。

The time-period is presented as follows (I can't change this): 时间段如下所示(我无法更改):

00:05:00 00:09:15 01:13:15 in an HH:MM:SS format. 00:05:00 00:09:15 01:13:15采用HH:MM:SS格式。

I want to do an _.filter() select on all objects with a date between for instance 00:00:00 and 00:10:00 , and then a filter with all objects between 00:10:00 and 00:20:00 . 我想对日期在例如00:00:0000:10:00之间的所有对象执行_.filter()选择,然后对所有对象在00:10:0000:20:00之间的对象进行过滤00:20:00

EDIT (clarification): With the time periods are 'times watched'. 编辑(说明):时间段为“观看时间”。 Let's say there are 50 entries of the time period, ranging from 00:00:00 to 1:30:00, but I want to make a new list that contains 假设该时间段有50个条目,范围从00:00:00到1:30:00,但是我想创建一个包含以下内容的新列表

  • The time period 时间段
  • The average 'times watched' in that time period. 该时间段内的平均“观看时间”。

This means I need some way to say: 'Select all time periods between zero minutes and 10 minutes. 这意味着我需要说些什么:'选择介于0分钟和10分钟之间的所有时间段。 Give me a list of those. 给我一个清单。 Compute the mean'. 计算均值”。

For that, I would need to have some possibility to do arithmetic operations, seeing I would like to 为此,我需要做一些算术运算,因为我想

  • Compare dates 比较日期
  • Add 10 minutes on each iteration 每次迭代增加10分钟

However, the Javascript Date object doesn't seem to handle 'just times' that well, neither does the moment.js library. 但是,Javascript Date对象似乎不能很好地处理“只是时间”, moment.js库也没有。 How would I go for this? 我该怎么办?

Here is how I would do in JS: 这是我在JS中要做的事情:

times=['00:05:00', '00:09:15', '01:13:15'];
  1. First get seconds 先得秒

     function getsec(time){ arr=time.split(':'); return (+arr[0])*3600 + (+arr[1])*60 + (+arr[2]); } times.map(getsec) [ 300, 555, 4395 ] 
  2. Then filter times into slots of particular time period 然后将时间过滤到特定时间段的广告位中

     function filter(last, curr, index, array){ chk=parseInt(curr/600); //slots for 10 minutes if (typeof last[chk] === 'undefined') { last[chk]=[]; last[chk].push(curr); } else last[chk].push(curr); return(last) } times.map(getsec).reduce(filter,[]); [ [ 300, 555 ], , , , , , , [ 4395 ] ] 

    Returns array of arrays. 返回数组的数组。 Each child array contains times within slots in increasing order 每个子数组在插槽中包含的时间按升序排列

  3. Calculate mean 计算平均值

     function avg(last, curr, index, array){ if(index==array.length-1) return (last+curr)/array.length; else return last+curr; } times.map(getsec).reduce(avg,0); 1750 function getavg(arr){ return arr.reduce(avg,0); } times.map(getsec).reduce(filter,[]).map(getavg); [ 427.5, , , , , , , 4395 ] 

    Since we are dealing with array of arrays we need a wrapper. 由于我们要处理数组数组,因此需要包装器。

Finally I would say that if you have many entries, databases would be faster. 最后我要说的是,如果您有很多条目,数据库将更快。

Simplest way would be for you to convert everything into seconds, and then do your thing. 最简单的方法是让您将所有内容转换为秒,然后执行操作。

inSeconds = function(time) {
  var secs, arr = [];
  arr = time.split(':').map(function(item) { return parseInt(item) });
  secs = arr[0]*3600 + arr[1]*60 + arr[2];
  return secs;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM