Tomorrows date?

1,156 views
Skip to first unread message

Calogero Buttaci

unread,
Jun 21, 2016, 6:00:50 PM6/21/16
to Node-RED
Hi, i'm using Node-Red on a Raspberry Pi3, Raspian Jessie. I'm definitely not a programmer.

I'm trying to display tomorrows date, i'm sure it's simple but i can't for the life of me figure this out.

I know new Date(); gives me todays date, Google is telling me to:

Calender cal = Calender.getInstance();
cal.add(Calender.DATE, 1);

But that just gives errors.

Any help would be welcomed,

Cheers
Charlie.

Nicholas O'Leary

unread,
Jun 21, 2016, 6:05:26 PM6/21/16
to Node-RED Mailing List
Hi Charlie,

The Date object constructor can also be passed a timestamp in milliseconds since epoch (Jan 1st, 1970) and it will create a Date object that represents that time, rather than 'now'.

var nowMS = Date.now();
var tomorrowMS = nowMS + 86400000; // <--- 24 hours in milliseconds
var tomorrowsDate = new Date(tomorrowMS);

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Calogero Buttaci

unread,
Jun 22, 2016, 3:51:38 AM6/22/16
to Node-RED
Hi Nick, thanks for prompt reply.

I was going to use this method but people online warned that it could give incorrect results due to daylight saving time, hence why they chose to use the Calendar class.

I suppose for what I need it'll be fine.

Thanks again, Charlie.

Walter Kraembring

unread,
Jun 22, 2016, 11:07:39 AM6/22/16
to Node-RED
Myself I have used the following in some function nodes (to get month+day) for today & tomorrow

Is this a bad and not recommended way?

    var d = new Date();
    var month = d.getMonth()+1;
    if (month < 10) {
        month = '0'+ month;
    }
    var day = d.getDate();
    if (day < 10) {
        day = '0'+ day;
    }
    
    d.setDate(d.getDate() + 1);
    var month_tomorrow = d.getMonth()+1;
    if (month_tomorrow < 10) {
        month_tomorrow = '0'+ month_tomorrow;
    }
    var day_tomorrow = d.getDate();
    if (day_tomorrow < 10) {
        day_tomorrow = '0'+ day_tomorrow;
    }
    
    var td = month+day;
    var tm = month_tomorrow+day_tomorrow;



Julian Knight

unread,
Jun 22, 2016, 11:48:49 AM6/22/16
to Node-RED
I strongly recommend using a JavaScript library to do date/time calculations. As you say, it is all too easy to fall foul of time-zone issues amongst other things.

moment.js is one of the best I've used. In fact, I used it in my node node-red-contrib-moment   ;)

Walter Kraembring

unread,
Jun 22, 2016, 2:05:10 PM6/22/16
to Node-RED
My godness, that is so powerful! The problem is to know everything that is already available...

Julian Knight

unread,
Jun 22, 2016, 2:49:24 PM6/22/16
to Node-RED
Ha ha, yes one of JavaScripts strengths and weaknesses perhaps.

Generally, you should assume that someone has beaten you to the punch when thinking of some knotty problem or algorithm. I've created many date/time utilities in years gone by for previous languages but the quality and depth of many of the JS libraries is pretty breathtaking.

Calogero Buttaci

unread,
Jun 22, 2016, 3:17:52 PM6/22/16
to Node-RED
Hi Julian,

Do you mean i can do the same with your node-red-contrib-moment node? If so i haven't been able to figure it out yet.

If not, how can i include the moment.js class in a function?

Cheers
Charlie.

Julian Knight

unread,
Jun 22, 2016, 6:21:32 PM6/22/16
to Node-RED
Ah, sorry to build your hopes up. I didn't really consider that use case.

Still it probably wouldn't be too hard to do. I'll give it some thought. In the mean time, you are totally free to pull a copy and see if you can work on it yourself ;) I'm happy to accept pull requests.

Julian Knight

unread,
Jun 26, 2016, 6:25:19 PM6/26/16
to Node-RED
If you are still interested, I've updated node-red-contrib-moment to allow for inputing dates like "tomorrow" as well as allowing to add/subtract timespans plus some other stuff.

Calogero Buttaci

unread,
Jun 27, 2016, 10:33:13 AM6/27/16
to Node-RED
Thanks Julian.

I managed to figure out how to use moment.js in a function node. I'll definitely update to your latest contrib and take a look.

Thanks for your hard work.

Charlie.

Walter Kraembring

unread,
Jun 27, 2016, 1:08:39 PM6/27/16
to Node-RED
Hello Julian,
I just would like to verify a thing with you. I have tested this in my system (a RPi). It looks like I am 2 hours off with your node compared what the system says. See picture below
I have also tried with the default settings (it gave Europe/Paris) but the same result

Kind regards, Walter

Julian Knight

unread,
Jun 28, 2016, 6:26:17 AM6/28/16
to Node-RED
Hi Walter, 

So CEST is GMT+2 so the default output, which is in ISO8601 format, is always in GMT (Zulu time) as indicated in the info sidebar. 

If you choose one of the other formats, you will see the output in local time. Try "LLL" as the format and see what happens.

BTW, If you are in Stockholm, I'm over there with the family in a month or so for our first holiday in Sweden! We've hired a motorhome for a couple of weeks to travel round Sweden and Norway.

Regards, Julian.
Kind regards, Walter
Reply all
Reply to author
Forward
0 new messages