简体   繁体   中英

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

 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 . Instead, since they're valid ISO-8601 date/time strings, parse them then add 36 minutes to them:

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, ...? If so, you can use the index that map passes as the second argument:

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. The above result in Date instances. If you want ISO strings instead, just call toISOString() on dt when returning it (see comments above).

Using 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 秒,然后将其转换回您的格式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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