简体   繁体   English

如何为每个下一个数组元素添加新的分钟值?

[英]How to add new minutes value for every next array element?

I want to add 36 minutes for every next value in the array but I get only one increase for all elements in array how to implement an algorithm which I describe above我想为数组中的每个下一个值增加 36 分钟,但数组中的所有元素只增加一次 如何实现我上面描述的算法

 let timestamps = [ "2020-01-21T22:36:00.000Z", "2020-01-21T23:12:00.000Z", "2020-01-21T23:48:00.000Z", "2020-01-22T00:24:00.000Z", "2020-01-22T01:00:00.000Z", ] const minutesToAdjust = 36 const millisecondsPerMinute = 60000 const oneDay = 1000 * 60 * 60 * 24 const twentyFourHours = new Date(new Date() - oneDay) const transformTimeseriesTo24h = timestamps.map(el => { el = new Date(twentyFourHours + (minutesToAdjust * millisecondsPerMinute)) return el }) timestamps = transformTimeseriesTo24h console.log(timestamps)

Your code is ignoring the original dates by immediately assigning to el .您的代码通过立即分配给el来忽略原始日期。 Instead, since they're valid ISO-8601 date/time strings, parse them then add 36 minutes to them:相反,由于它们是有效的 ISO-8601 日期/时间字符串,解析它们然后向它们添加 36 分钟:

timestamps = timestamps.map(el => {
    const dt = new Date(el);
    dt.setMinutes(dt.getMinutes() + 36); // Will wrap for you
    return dt; // Or `return dt.toISOString();`
});

Live Example:现场示例:

 let timestamps = [ "2020-01-21T22:36:00.000Z", "2020-01-21T23:12:00.000Z", "2020-01-21T23:48:00.000Z", "2020-01-22T00:24:00.000Z", "2020-01-22T01:00:00.000Z", ]; timestamps = timestamps.map(el => { const dt = new Date(el); dt.setMinutes(dt.getMinutes() + 36); // Will wrap for you return dt; // Or `return dt.toISOString();` }); console.log(timestamps);

Or... "Every next value" sounds like you want to add 0 to the first one, 36 minutes to the second one, 72 (36 * 2) minutes to the third, ...?或者......“每个下一个值”听起来像是你想在第一个值上加 0,在第二个值上加 36 分钟,在第三个值上加 72 (36 * 2) 分钟,......? If so, you can use the index that map passes as the second argument:如果是这样,您可以使用map传递的索引作为第二个参数:

timestamps = timestamps.map((el, index) => {
    const dt = new Date(el);
    dt.setMinutes(dt.getMinutes() + (index * 36)); // Will wrap for you
    return dt; // Or `return dt.toISOString();`
});

Live Example:现场示例:

 let timestamps = [ "2020-01-21T22:36:00.000Z", "2020-01-21T23:12:00.000Z", "2020-01-21T23:48:00.000Z", "2020-01-22T00:24:00.000Z", "2020-01-22T01:00:00.000Z", ]; timestamps = timestamps.map((el, index) => { const dt = new Date(el); dt.setMinutes(dt.getMinutes() + (index * 36)); // Will wrap for you return dt; // Or `return dt.toISOString();` }); console.log(timestamps);


I couldn't tell whether you wanted to end up with Date instances of ISO strings.我不知道您是否想要以 ISO 字符串的Date实例结束。 The above result in Date instances.上面的结果是Date实例。 If you want ISO strings instead, just call toISOString() on dt when returning it (see comments above).如果你想要 ISO 字符串,只需在返回它时在dt上调用toISOString() (见上面的评论)。

Using Date.parse(el)使用Date.parse(el)

 let timestamps = [ "2020-01-21T22:36:00.000Z", "2020-01-21T23:12:00.000Z", "2020-01-21T23:48:00.000Z", "2020-01-22T00:24:00.000Z", "2020-01-22T01:00:00.000Z", ] const minutesToAdjust = 36 const millisecondsPerMinute = 60000 const oneDay = 1000 * 60 * 60 * 24 const twentyFourHours = new Date(new Date() - oneDay) const transformTimeseriesTo24h = timestamps.map(el => { return new Date(Date.parse(el) + (minutesToAdjust * millisecondsPerMinute)) }) timestamps = transformTimeseriesTo24h console.log(timestamps)

You need to use timestamp value from array and add your offset in that您需要使用数组中的时间戳值并在其中添加偏移量

 let timestamps = [ "2020-01-21T22:36:00.000Z", "2020-01-21T23:12:00.000Z", "2020-01-21T23:48:00.000Z", "2020-01-22T00:24:00.000Z", "2020-01-22T01:00:00.000Z", ]; const minutesToAdjust = 36 const millisecondsPerMinute = 60000 const oneDay = 1000 * 60 * 60 * 24 const twentyFourHours = new Date(new Date() - oneDay) timestamps = timestamps.map(time => new Date(new Date(time).getTime() + minutesToAdjust * millisecondsPerMinute)); console.log(timestamps)

我会将您的时间戳转换为Unix 时间,向其中添加 36*60 秒,然后将其转换回您的格式。

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

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