[SOLVED] JSR223 date format - how?

Hey,

how do i format the current time to GMT 0 timezone with using the NGRE in javascript?

this is what i have now

'use strict';

load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/lib/javascript/core/rules.js');

var me = "003_test_date_format.js";

JSRule({
	name: "Test Date Format",
	description: "Line: "+__LINE__,
	triggers: [ 
		TimerTrigger("0/15 * * * * ?") // 15 seconds
	],
    execute: function( module, input){
        var now = new Date().toUTCString();
        //var format = now.format("yyyy-MM-dd");
        logInfo("Date Format: ", now);
    }
});

which gives me in the log:

'Thu, 30 May 2019 09:08:45 GMT'

thanks for your help :slight_smile:

EDIT.
found a way to get the current time in GMT

So the questions is now… how to format that in “YYYY-MM-DD HH:MM” ?

i got it working…

var format = now.getUTCFullYear() + "-" + now.getUTCMonth() + "-" + now.getUTCDate() + " " + now.getUTCHours() + ":" + now.getUTCMinutes() + ":" + now.getUTCSeconds();

i mark as solved…
If you know another/ better way, please let me know.

Edit: 31.05.2019
Just fot the record:
the above version was working but gave me a format which i cant use…
I’ve now accomblished it by using:

var gmtdate = now.toJSON();

which is resulting in:

2019-05-31T18:43:54.392Z

Thanks

1 Like