# Help with javascript function node

**URL:** <https://discourse.nodered.org/t/help-with-javascript-function-node/84164>\
**Category:** General\
**Created:** [1 January 2024 04:28 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164 "2024-01-01T04:28:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gibbz](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Gibbz](https://discourse.nodered.org/u/Gibbz)\
**Post date:** [1 January 2024 04:28 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164/1 "2024-01-01T04:28:42Z")

</div>

Im trying to use a forumla to calc sunset. However when I save it, it has an error on line 9.

```auto
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);

```

Its complaing that now and start are the wrong type. But when i run the code in js it works fine.... How do i change this to work with node red?

See the function code:

```auto
function sunriseset(la, lo, sunrise) {

    const now = new Date();

    const xmonth = now.getMonth() + 1;
    const xyear = now.getFullYear();

    var start = new Date(now.getFullYear(), 0, 0);
    var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);

    const zenith = 90.83333333333333;
    const D2R = Math.PI / 180;
    const R2D = 180 / Math.PI;

    // convert the longitude to hour value and calculate an approximate time
    const lnHour = lo / 15;
    let t;
    if (sunrise) {
        t = day + ((6 - lnHour) / 24);
    } else {
        t = day + ((18 - lnHour) / 24);
    }

    //calculate the Sun's mean anomaly
    const M = (0.9856 * t) - 3.289;

    //calculate the Sun's true longitude
    let L = M + (1.916 * Math.sin(M * D2R)) + (0.020 * Math.sin(2 * M * D2R)) + 282.634;
    if (L > 360) {
        L = L - 360;
    }
    else if (L < 0) {
        L = L + 360;
    }

    //calculate the Sun's right ascension
    let RA = R2D * Math.atan(0.91764 * Math.tan(L * D2R));
    if (RA > 360) {
        RA = RA - 360;
    }
    else if (RA < 0) {
        RA = RA + 360;
    }

    //right ascension value needs to be in the same qua
    const Lquadrant = (Math.floor(L / (90))) * 90;
    const RAquadrant = (Math.floor(RA / 90)) * 90;
    RA = RA + (Lquadrant - RAquadrant);

    //right ascension value needs to be converted into hours
    RA = RA / 15;

    //calculate the Sun's declination
    const sinDec = 0.39782 * Math.sin(L * D2R);
    const cosDec = Math.cos(Math.asin(sinDec));

    //calculate the Sun's local hour angle
    const cosH = (Math.cos(zenith * D2R) - (sinDec * Math.sin(la * D2R))) / (cosDec * Math.cos(la * D2R));
    let H;
    if (sunrise) {
        H = 360 - R2D * Math.acos(cosH);
    } else {
        H = R2D * Math.acos(cosH)
    }
    H = H / 15;

    //calculate local mean time of rising/setting
    const T = H + RA - (0.06571 * t) - 6.622;

    //adjust back to UTC
    let UT = T - lnHour;
    if (UT > 24) {
        UT = UT - 24;
    }
    else if (UT < 0) {
        UT = UT + 24;
    }

    const ms = UT * 60 * 60 * 1000;

    const time = new Date(ms);
    time.setFullYear(now.getFullYear());
    time.setMonth(now.getMonth());
    time.setDate(now.getDate());
    return time;
}

// sunset
var lat = -34.92866000
var lon = 138.59863000
var sunset = sunriseset(lat, lon, false)

msg.payload = sunset
return msg;

```

---

<div class="post-metadata">

**Author:** ![Gibbz](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Gibbz](https://discourse.nodered.org/u/Gibbz)\
**Post date:** [1 January 2024 04:31 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164/2 "2024-01-01T04:31:05Z")

</div>

okay, so the code runs in node red just fine! just has this error all the time, how can i clear the error?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 January 2024 06:46 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164/3 "2024-01-01T06:46:04Z")

</div>

At a guess...

```auto
const diff = (now.valueOf() - start.valueOf()) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);

```

---

<div class="post-metadata">

**Author:** ![Gibbz](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Gibbz](https://discourse.nodered.org/u/Gibbz)\
**Post date:** [1 January 2024 07:46 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164/4 "2024-01-01T07:46:57Z")

</div>

awesome thats fixed it 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 January 2024 12:07 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164/5 "2024-01-01T12:07:55Z")

</div>

I assume you are aware that there are multiple nodes that will give you different astronomical times - including sunrise/set for your location?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 January 2024 12:08 UTC](https://discourse.nodered.org/t/help-with-javascript-function-node/84164/6 "2024-01-15T12:08:30Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
