Date Time retrieved from item is in micros seconds and date.Parse doesn't work

I am trying to get epoch time of a datetime item so I can compare time date differences between the items etc.
I am using javascript and an I am on OpenHab 3.3.0

The issue I see is the date/time returned is in microseconds and the date.Parse doesn’t seem to like that.
If I remove the microseconds from the datetime then it seems to work.
Am I doing it correctly or not?

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);

var datework     = Date.parse("2022-10-27T13:12:36.299+1100"); //<<this has microseconds removed
var datenotwork  = Date.parse("2022-10-27T13:12:36.299631+1100") ; //<<this is the device date time returned
var devicedate   = (items["Zigbeesengledbulb_Zigbeesengledlastseen"]); //<<this is the returned value

logger.info("\n\r devicedate = " + devicedate + "\n\r datenotwork  " + datenotwork + "\n\r datework  " + datework);

Below is the log output:

2022-10-27 13:39:52.712 [INFO ] [org.openhab.rule.886b62c8a4         ] - 
 devicedate = 2022-10-27T13:32:46.081557+1100
 datenotwork  NaN
 datework  1666836756299

I always seem to struggle with dates and differences.
Is there an easier way to compare dates and times?
Thanks in advance.

Which JavaScript?

The state of a DateTime Item is a DateTimeType which carries a java.time.ZonedDateTime. Since the purpose of rules is to interact with openHAB and all your interactions with openHAB will require a ZonedDateTime, why convert it? It’s usually better to keep it as a ZonedDateTime.

Yes. Assuming ECMAScript 5, stay in the java.time.* classes.

To compare two ZonedDateTimes:

if(myTime1.isBefore(myTime2))

if(myTime2.isAfter(myTime1))

To find out the difference between two date times:

Duration.between(myTime1, myTime2)

See Duration (Java SE 11 & JDK 11 ) for all the things you can do with a Duration.

If you are using JS Scripting ECMAScript 11, see Working with Date Times in JS Scripting (ECMAScript 11)

To a large extent, if you are trying to convert things to a JavaScript Date or converting things to epoch, you are doing it the hard way. Date times are hard enough. Use the tools available to you to stay at a higher level.

Thanks for the reply. I am using ECMAScript 11 and I have read those documents.
I am not a programmer. I just wanted an example that I could cut and paste and use but I am unable to find anything that works.

For example if I use:
var datework = time.toZDT().betweenTimes(‘10:00 PM’, ‘8:00 AM’)

I get error:
2022-10-28 08:49:35.543 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘886b62c8a4’ failed: ReferenceError: “time” is not defined in at line number 7

Thanks again. I will try to find something that makes sense to me.

Beware cargo cult programming. It pays to have at least a basic understanding of the code you are copying.

Are you? Did you change the settings? If you didn’t change the settings, you got that error because you are using ECMAScript 5.1. If you only have one choice it’s because you haven’t installed the JS Scripting add-on under automation.

OK…That I didn’t know about the JS addon. I assumed if I upgraded OH then the scripting was done as well.
I will delve into this later.
Now I know why nothing worked as I was looking et the ECMA 11 examples.

Thanks