Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 0UovZFeD58wvyyGjyyIPio
Power Automate - Power Automate Desktop
Answered

Unix Time Conversion?

Like (0) ShareShare
ReportReport
Posted on 13 Nov 2023 22:53:31 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. ☹

  • BrendonKoz Profile Picture
    60 on 15 Nov 2023 at 18:30:37
    Re: Unix Time Conversion?

    🤦‍♂️ 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.

  • yoko2020 Profile Picture
    495 on 15 Nov 2023 at 06:31:29
    Re: Unix Time Conversion?

    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 14 Nov 2023 at 22:30:09
    Re: Unix Time Conversion?

    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.

  • UshaJyothiKasibhotla Profile Picture
    6 Super User 2025 Season 1 on 14 Nov 2023 at 05:19:21
    Re: Unix Time Conversion?

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

  • Verified answer
    yoko2020 Profile Picture
    495 on 14 Nov 2023 at 03:18:33
    Re: Unix Time Conversion?

    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")

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Power Automate Desktop

#1
eetuRobo Profile Picture

eetuRobo 4 Super User 2025 Season 1

#2
KO-05050229-0 Profile Picture

KO-05050229-0 2

#2
stampcoin Profile Picture

stampcoin 2

Overall leaderboard