VMware Cloud Community
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Convert dates in embedded vRO 8 with offset. Appliances use etc/utc.

I'm curious how others deal with the embedded vRO with vRA since the appliance is set to etc/utc.  The date object in vRO doesn't seem to support this code:

 

var stringInput = "2021-09-23T23:13:58.166554Z";
var timeZone = "America/Los_Angeles";
var dateObject = new Date(stringInput).toLocaleString("en-US", {timeZone});
 
This works great outside of vRO.  I guess I can manually set it to -7 hours but I like the above much better.  All of the dates saved in vRA objects are also in etc/utc so I need to convert those as well.
1 Solution

Accepted Solutions
xian_
Expert
Expert
Jump to solution

you can try momentjs

https://communities.vmware.com/t5/vRealize-Orchestrator/solved-How-to-convert-a-Date-from-one-Timezo...

 

var moment = System.getModule("com.test.momentjs").getMomentInstance();
var momentTZ = System.getModule("com.test.momentjs").getMomentTZ(moment);

var d = moment("2021-09-23T23:13:58.166554Z");
System.log(d.format());
System.log(d.tz("America/Los_Angeles").format("L, LTS"));

---

[2022-02-10 13:19:45.294] [I] 2021-09-23T23:13:58+00:00
[2022-02-10 13:19:45.301] [I] 09/23/2021, 4:13:58 PM

View solution in original post

8 Replies
xian_
Expert
Expert
Jump to solution

you can try momentjs

https://communities.vmware.com/t5/vRealize-Orchestrator/solved-How-to-convert-a-Date-from-one-Timezo...

 

var moment = System.getModule("com.test.momentjs").getMomentInstance();
var momentTZ = System.getModule("com.test.momentjs").getMomentTZ(moment);

var d = moment("2021-09-23T23:13:58.166554Z");
System.log(d.format());
System.log(d.tz("America/Los_Angeles").format("L, LTS"));

---

[2022-02-10 13:19:45.294] [I] 2021-09-23T23:13:58+00:00
[2022-02-10 13:19:45.301] [I] 09/23/2021, 4:13:58 PM

qc4vmware
Virtuoso
Virtuoso
Jump to solution

Wow... I find it a bit disappointing that this may be the answer.  Thanks for pointing me to this article though!  Considering VMware has products where they want etc/utc set as the appliance time zone you'd think they would have a solution for generating dates in a local or other time zone easily.  Especially since it seems this is easily done with basic javascript but their implementation isn't allowing for it?  Maybe I am missing something since I am not in anyway an expert in javascript.

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

@xian_ do you have this working?  I tried downloading the package but it downloads corrupted for me at least when I try to import to vRA 7.6 or vRA 8.6.2.  If so can you repost a package in this thread?

0 Kudos
xian_
Expert
Expert
Jump to solution

Unfortunately I do not have mosestef's package, I simply recreated the actions based on @eoinbyrne 's instructions.

I attach my package that I got working.

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Oh awesome... for some reason I thought the instructions were just on using his package.  Thanks for sharing.  I also came up with a work around calling the linux date command line using the epoch of the date but much prefer to keep it all in vRO and not have to use an external host.

 

TZ=":America/Los_Angeles" date -d @1644521536 +'%xT%T' will output in the specified timezone in whatever way you want to format it.  I just formatted it in something I could easily parse similar to other date strings I've seen.  That commands output looks like this "02/10/2022T11:32:16"

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

I'm now realizing what I thought was standard javascript I mentioned earlier was actually an example from luxon https://moment.github.io/luxon/#/?id=luxon .  I wonder if luxon can be pulled into vRO like momentsjs was?  Sounds like momentsjs is deprecated but this all works great for my needs.  This should really be better supported in vRO though they should have some sort of equivalent.  Still hoping I'm just missing something.

0 Kudos
xian_
Expert
Expert
Jump to solution

The benefit of deprecated JS implementations are that they are more likely to run on vRO's old ES5 JS engine. Luxon may run but one has to try it...

Another approach is using vRO polyglot capabilities and run JS code with the NodeJS engine. I found these FaaS actions starting slower as they deployed into containers.

vRO can parse your date string with:

d = System.getDateFromFormat(stringInput.substr(0,23), "yyyy-MM-dd'T'HH:mm:ss.SSS");

but I found no TZ support for displaying it other than the default timezone.

qc4vmware
Virtuoso
Virtuoso
Jump to solution

The moments.js is working great so I'll stick with that for now!  Great idea on FaaS.  I forgot that was now an option.  Maybe at some point in the future I'll try that.

0 Kudos