user10717010
user10717010

Reputation:

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)

Upvotes: 0

Views: 90

Answers (4)

Aerials
Aerials

Reputation: 4419

I would convert your time stamps to Unix time add the 36*60 seconds to it, and convert it back to your format.

Upvotes: 0

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22885

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)

Upvotes: 0

User863
User863

Reputation: 20039

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)

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074475

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

Upvotes: 0

Related Questions