web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Automate
Unanswered

Unix Time Conversion?

(0) ShareShare
ReportReport
Posted on by 60

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. ☹

I have the same question (0)
  • Verified answer
    yoko2020 Profile Picture
    495 on at

    This is using powershell

    $unixTimestamp = 1699765200 # Replace this with your Unix timestamp
    
    # Convert Unix timestamp to DateTime
    $dateTime = [System.DateTimeOffset]::FromUnixTimeSeconds($unixTimestamp).DateTime
    
    # Display the result in a specific format (adjust the format as needed)
    $dateTime.ToString("MM-dd-yyyy HH:mm:ss")
  • UshaJyothiKasibhotla Profile Picture
    203 Moderator on at

    Have you tried converting date time to text and converting text to DateTime ?

  • BrendonKoz Profile Picture
    60 on at

    Well, not only did Powershell work, it was excrutiatingly faster. I don't dabble in Powershell too often, I didn't realize it had a date library. I also (wrongly) assumed running scripts in those languages would call a static file, not inline. Not sure why I thought that (I could've likely used Python). I got hyperfocused on the JavaScript method, I guess. Thanks for the suggestion and, in this case, solution!

     

    I had tried converting text to a Datetime value, but the text value I had was the timestamp value (from a REST API) and PAD was unable to recognize it.

  • yoko2020 Profile Picture
    495 on at

    Glad it helps.

    For your reference
    https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimemilliseconds?view=net-7.0

    https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimeseconds?view=net-7.0


    PAD v 2.38 has new features :

    New Features
    Natural Language to script is now available in preview for the following scripting actions:

    Run DOS command
    Run VBScript
    Run JavaScript
    Run PowerShell script
    Run Python script

     

    yoko2020_0-1700030164799.png

     

  • BrendonKoz Profile Picture
    60 on at

    🤦‍♂️ Copilot... I should've thought to try that too...! Thank you for the links to the references as well. I did have to make a tiny adjustment to the code to convert the output to my local timezone, and adjust the time formatting, so using the references to know what methods exist, and then copilot to quickly ask for a reformat and I lazily, and quickly, arrived to the right solution. My primary coding languages are (non-.NET) web-based, so PowerShell and an integration of .NET within PowerShell just went over my head. (Searching examples for how to convert UNIX time to DateTime in PowerShell look nothing like that - far more streamlined - version you provided.)

     

    Seriously. Thank you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard