Help us understand the problem. What is going on with this article?

Unix timestampとDate time間の変換 #PowerAutomate

Unix timestampとdate timeを変換する処理をPower Automateで結構利用するので、備忘録として残しておきます。

Date time → Unix timestamp

入力がDate time (yyyy-mm-dd等)でUnix timestamp (秒単位)にしたい場合には

div(sub(ticks('ここにDate time'),ticks('1970-01-01')), 10000000)

例えばConvert time zoneの結果を使う場合には、

div(sub(ticks(body('Convert_time_zone')),ticks('1970-01-01')), 10000000)

ticks関数は西暦を100ナノ秒単位で返す関数なので、これを利用して、1970-01-01におけるナノ秒との差分を求めて、その後ナノ秒→秒にするために割り算する

Unix timestamp → Date timeの変換

入力が秒単位のUnix timestampで、これをDate timeに直す場合には、

addseconds('1970-1-1', ここにUnix timestamp (数値), 'yyyy-MM-dd HH:mm')

もとの数式は addSeconds('<timestamp>', <seconds>, '<format>'?) この書式なので、1970-01-01にUnix timeを足せば、Date timeに指定のフォーマットで変換できる

動作確認

実際Convert time zoneでutcNow()をJSTに変換したあとにUnix timestampに変換し、その結果を再変換してみると以下のようになる。

image.png

Reference

Ticks関数: https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#ticks

addSeconds関数: https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#addSeconds

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした