7

整理幾個 Azure Logic Apps 與 Power Automate 會用到的日期函式

 3 years ago
source link: https://blog.miniasp.com/post/2021/07/04/Common-Date-functions-in-expressions-for-Azure-Logic-Apps-and-Power-Automate
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

最近熱愛 low-codeno-code 的解決方案,簡單拖拉幾個設定,或是複製之前寫好的 JSON,就可以快速完成一個日常的自動化工作,減少越來越多的人工操作,重點是幾乎不用花錢,也不用花心思在部署上。今天我要來整理幾個 Azure Logic AppsPower Automate 會用到的日期函數,因為我的自動化工作大多都跟日期時間有點關係。

Azure Logic App 的 Consumption Plan 每個月有 4,000 次觸發動作是完全免費的!

基本「日期」觀念

其實在 Azure Logic AppsPower Automate 裡面並沒有「日期」型別,所有的 timestamp 都是以 ISO 8601 的「字串」來表示。

  • 表達一份 UTC 日期時間格式

    2021-07-04T03:30:08.9644580Z
    

    日期與時間之間會有個 T 字元,結尾有個 Z 字元,就代表這是一個 UTC 時間!

  • 轉換一份無法判斷的日期時間格式

    如果你有個字串為 10212011,雖然人眼看的出是 月日年 (MMddyyyy),但是系統卻無法正確解析,此時就可以利用字串串接的技巧,重新格式化成看的懂的格式 (MM-dd-yyyy),再透過 formatDateTime 轉換成你想要轉換過去的格式 (yyyy-MM-dd)。

    formatDateTime(concat(substring('10212011', 0,2), '-', substring('10212011', 2,2), '-', substring('10212011', 4,4)), 'yyyy-MM-dd')
    

    注意:如果 formatDateTime 函式沒有加上第二個 format 參數,預設輸出的格式為 yyyy-MM-ddTHH:mm:ss.fffffffK

  • 轉換一份 Epoch time (Unix time) 時間為 timestamp

    Epoch 時間又稱 Unix time,一般來說都是以 UTC 時間來計算的,而且都是從 1970-01-01T00:00:00Z 到現在時間的 Seconds (秒數)!

    所以你可以用 addSeconds 函式來將一個數值轉換成 timestamp 格式:

    addSeconds('1970-01-01T00:00:00Z', 1625384178)
    

    偶爾會有人將 Epoch time 改用 Milliseconds (豪秒) 單位,這時就要記得先除以 1000 才能進行轉換:

    addSeconds('1970-01-01T00:00:00Z', div(1625384178964, 1000))
    

    這裡有個好用的 Epoch & Unix Timestamp Conversion Tools 可以幫你轉換 Epoch 時間格式。另外,如果你想在 Linux 環境下快速取得當下的 Epoch 時間,可以使用 date +%s (秒) 或 date +%s%3N (豪秒) 命令!

常見的日期函式

  • 取得 UTC 當前時間

    utcNow()
    
    2021-07-04T03:30:08.9644580Z
    
  • 取得台北標準時間當前時間

    convertFromUtc(utcNow(), 'Taipei Standard Time')
    
    2021-07-04T11:30:08.9644580
    
  • 指定輸出特定日期時間格式(台北標準時間)- 日期部分

    formatDateTime(convertFromUtc(utcNow(), 'Taipei Standard Time'), 'yyyy-MM-dd')
    
    2021-07-04
    

    自訂日期格式的語法詳見 Custom date and time format strings 文件。

  • 指定輸出特定日期時間格式(台北標準時間)- 時間部分

    formatDateTime(convertFromUtc(utcNow(), 'Taipei Standard Time'), 'hh:mm:ss')
    
    11:30:08
    
  • 取得今天星期幾(台北標準時間

    dayOfWeek(convertFromUtc(utcNow(), 'Taipei Standard Time'))
    
    0
    

    注意:星期天0星期一1,依此類推,星期六6

  • 取得當月的第一天 (台北標準時間

    formatDateTime(startOfMonth(convertFromUtc(utcNow(), 'Taipei Standard Time')), 'yyyy-MM-dd')
    
    2021-07-01
    
  • 取得次月的第一天 (台北標準時間

    formatDateTime(startOfMonth(addToTime(convertFromUtc(utcNow(), 'Taipei Standard Time'), 1, 'month')), 'yyyy-MM-dd')
    
    2021-08-01
    
  • 取得當月的最後一天 (台北標準時間

    formatDateTime(subtractFromTime(startOfMonth(addToTime(convertFromUtc(utcNow(), 'Taipei Standard Time'), 1, 'month')), 1, 'second'), 'yyyy-MM-dd')
    
    2021-07-31
    

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK