Hi all - when searching for solutions for converting a Unix timestamp into a datetime object, I keep running into solutions for other Power Automate solutions, not Desktop; and I'm not seeing a simple way to convert within PAD. I thought I had a solution with some Add to datetime trickery by starting at January 1, 1970 ... and although that worked initially, I then ran into Daylight Savings Time...which broke everything.
As an alternative solution, I thought I could use JavaScript to properly format the values to whatever datetime type, or string value I'd want - unfortunately I'm having a lot of trouble with that approach. Run JavaScript doesn't seem to return the string formatting I would expect, and Run JavaScript function on web page isn't working for me, I'm getting the returned value of "[object Object]" which I know means there was an error in the JS, but I've been unable to find it; it runs fine in the browser's developer console (and I'm not sure how to debug browser JS injected by PAD -- how is this done?). I think it's something to do with trying to inject a PAD variable into the JS, but only for the [...] function on web page call, but I'm not 100% sure...
Any hints here?
For reference (I have two timestamps, and am trying to end up with a date, and a time from each timestamp):
Run Javascript:
var datetimeStart = '%StartDatetime%';
var date = new Date(datetimeStart * 1000)
WScript.Echo(date.toLocaleDateString('en-US'));
This returns (with a %StartDatetime% value of: 1699765200) "Sunday, November 12, 2023". With the locale of 'en-US' I'd expect that to be "11/12/2023". Using formatting for the time, I'm also getting the full "Sunday, November 12, 2023" instead of a textual string representing the time. Example:
var datetimeStart = '%StartDatetime%';
var date = new Date(datetimeStart * 1000)
var options = { hour: 'numeric', minute: 'numeric', hour12: true };
WScript.Echo(date.toLocaleDateString('en-US', options));
I figured maybe there's a compatibility with the JS engine used in PAD and a standard web browser's JS engine, so decided to take advantage of the fact that I'm already interacting with a web browser, and used Run JavaScript function on web page since I knew that would work at least when run in a browser's console.
Run JavaScript function on web page:
function ExecuteScript() {
var datetimeStart = '%StartDatetime%';
var date = new Date(datetimeStart * 1000);
var startDateInput = document.getElementById('EventDate');
var dateString = date.toLocaleDateString('en-US');
startDateInput.value = dateString;
// return date.toLocaleDateString('en-US');
if (dateString == null) {
return("undefined")
} else {
return(JSON.stringify(dateString);
}
// return dateString;
}
This returns: "[object Object]", even if I try simply sending back "test" as a return value. I tried setting the input field's value in the JavaScript here too, just in case, but that didn't do anything. The bottom if/else was an attempt to parse a returned object, but I'm pretty sure the object being returned is just an error, not an intended value. ☹