# Set context array not working

**URL:** <https://discourse.nodered.org/t/set-context-array-not-working/12829>\
**Category:** General\
**Created:** [2 July 2019 13:58 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829 "2019-07-02T13:58:02Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Zwall33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zwall33/32/9814_2.png) [@Zwall33](https://discourse.nodered.org/u/Zwall33)\
**Post date:** [3 July 2019 08:59 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829/21 "2019-07-03T08:59:42Z")

</div>

I get this

```auto
date isobject: Wed Jul 03 2019 11:00:32 GMT+0200 (CEST)
typeof a is object

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 July 2019 09:03 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829/22 "2019-07-03T09:03:02Z")

</div>

I don't see in the docs for javascript `new Date()` where it says you can pass it an object.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [3 July 2019 09:06 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829/23 "2019-07-03T09:06:06Z")

</div>

> [@Zwall33](#):
>
> The node who doesn't work

And what exactly doesn't work?

---

<div class="post-metadata">

**Author:** ![Zwall33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zwall33/32/9814_2.png) [@Zwall33](https://discourse.nodered.org/u/Zwall33)\
**Post date:** [3 July 2019 09:12 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829/24 "2019-07-03T09:12:25Z")

</div>

```auto
function GetTime (date){
    var a = new Date(date);
    node.warn("date is" + typeof date +": "+ date);
    node.warn("typeof a is " + typeof a );
    node.error(a);
    h = a.getHours();
    m = a.getMinutes();
    s = a.getSeconds();
    var t = [h,m,s];
    return t;
}

```

This, I can't get it to work... I get `TypeError: Cannot set property '0' of undefined`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 July 2019 09:16 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829/25 "2019-07-03T09:16:15Z")

</div>

As I said, I can't see in the docs for new Date() where it says you can pass it an object.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [3 July 2019 09:20 UTC](https://discourse.nodered.org/t/set-context-array-not-working/12829/26 "2019-07-03T09:20:16Z")

</div>

> [@Zwall33](#):
>
> This, I can't get it to work... I get `TypeError: Cannot set property '0' of undefined`

That error does not come out of that function. You are not trying to set a property called `0` in that code. It must be coming from somewhere else in your function.

Looking through your code, I can see you have this for loop:

```auto
        for(j = 0; j < StartDiff.length; j++){
            timeDef[j] = EndDiff[j] - StartDiff[j];
        }

```

But you have not defined `timeDef` anywhere. You do have `var timeDef` at the top, but it isn't initialised to anything - so will be undefined. That would certainly be one cause of the error.

But stepping back from that, it looks like you are trying to calculate the different between a start time and an end time.

Your code turns a Date object into an array `[hh,mm,ss]` and then it calculates the different between each of those individual values and joins them into a single string.

That is not going to give you a value that you can use.

If the start time is 12:34:56 and the end time is 13:21:50, you will end up with the arrays `[12,34,56]` and `[13,21,50]`. When you calculate `timeDef` you will get `[1,-13,-6]`. When you join those into a string, you'll get `"1-13-6"` which is pretty meaningless.

To calculate time differences, don't try to split it up like that.

Given `startTime` and `endTime` as Date objects, a much better way to calculate the difference is to do:

```auto
var diff = endTime.getTime() - startTime.getTime();

```

That will return the number of milliseconds between the two times. You can then do some simple maths to convert that into hours/minutes/seconds if you really want to.

[Previous page](https://discourse.nodered.org/t/set-context-array-not-working/12829.md?page=1)
