此文件涵蓋了涉及 DateTimeOffset 和期間的操作和轉換,包括格式化、計算、類型檢查以及在文本、數字和各種單位之間的轉換。
DateTimeOffset
將文本轉換為 DateTimeOffset 格式
暫停到 DateTimeOffset
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>jsonValue</variable></variables><block type=\"DateTime_parseDateTimeOffset\" disabled=\"true\" x=\"-22\" y=\"112\"><value name=\"s\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}
此塊用於將文本分析到 DateTimeOffset 中。
參數: 字串
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_parseDateTimeOffset\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">2022-08-16</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
生成具有不同單位的 DateTimeOffset
使用年份創建 DateTimeOffset
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_createDateTimeOffset\" disabled=\"true\" x=\"-22\" y=\"202\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"0\" _input_init=\"false\"></mutation><value name=\"year\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">2020</field></shadow></value></block></xml>"}
此塊用於隨時間生成新的 DateTimeOffset。
參數:數字(預設值:2020)
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDateTimeOffset\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"6\" _input_init=\"true\"></mutation><value name=\"year\"><shadow type=\"math_number\"><field name=\"NUM\">2022</field></shadow></value><value name=\"month\"><shadow type=\"math_number\"><field name=\"NUM\">8</field></shadow></value><value name=\"day\"><shadow type=\"math_number\"><field name=\"NUM\">2</field></shadow></value><value name=\"hour\"><shadow type=\"math_number\"><field name=\"NUM\">11</field></shadow></value><value name=\"minute\"><shadow type=\"math_number\"><field name=\"NUM\">8</field></shadow></value><value name=\"second\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"offset\"><shadow type=\"text\"><field name=\"TEXT\">+23:35</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
使用 Unix 時間生成 DateTimeOffset
從 Unix 時間創建 DateTimeOffset,以毫秒為單位
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_fromUnixTimeMilliseconds\" disabled=\"true\" x=\"-22\" y=\"247\"><value name=\"milliseconds\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}
此塊用於使用 Unix 時間(以毫秒為單位)生成新的 DateTimeOffset。
參數: 數字(預設值:0)
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_fromUnixTimeMilliseconds\"><value name=\"milliseconds\"><shadow type=\"math_number\"><field name=\"NUM\">500</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
使用當前時間顯示 DateTimeOffset
使用當前時間創建 datetimeoffset
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_now\" disabled=\"true\" x=\"-22\" y=\"337\"/></xml>"}
此塊用於使用當前時間生成新的 DateTimeOffset。
參數: N/A
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
將新偏移量與 DateTimeOffset 合併
將 DateTimeOffsest 轉換為偏移量
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_convertOffset\" disabled=\"true\" x=\"-22\" y=\"382\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"offset\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">+00:00</field></shadow></value></block></xml>"}
此塊用於將 DateTimeOffset 與新的偏移量組合在一起。
參數:
- DateTimeOffset
- 字串
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"DateTime_DateTimeOffset_convertOffset\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"offset\"><shadow type=\"text\"><field name=\"TEXT\">+08:00</field></shadow></value></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
修改偏移量
將 dateTimeOffset 的偏移量設置為
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_setOffset\" disabled=\"true\" x=\"-22\" y=\"472\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"offset\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">+00:00</field></shadow></value></block></xml>"}
此塊用於設置與原始 DateTimeOffset 的新偏移量。
參數:
- DateTimeOffset
- 字串
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"DateTime_DateTimeOffset_setOffset\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"offset\"><shadow type=\"text\"><field name=\"TEXT\">+07:00</field></shadow></value></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
將 DateTimeOffset 的格式更改為文字格式
dateTimeOffset 轉換為格式為字串
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_toFormat\" disabled=\"true\" x=\"-22\" y=\"427\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"format\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">yyyy-MM-dd HH:mm:ss ZZ</field></shadow></value></block></xml>"}
此塊用於將 DateTimeOffset 更改為特定的字串格式。
參數:
- DateTimeOffset
- 字串
輸出: DateTimeOffset
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toFormat\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"format\"><shadow type=\"text\"><field name=\"TEXT\">yyyy-MM-dd HH:mm:ss ZZ</field></shadow></value></block></value></block></next></block></statement></block></xml>"}
輸出:
將 DateTimeOffset 轉換為文字格式
dateTimeOffset 轉換為字串
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_toString\" disabled=\"true\" x=\"-22\" y=\"248\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此塊用於將 DateTimeOffset 轉換為字串。
參數: DateTimeOffset
輸出: 字串
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"20\" y=\"20\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
計算兩個 DateTimeOffsets 之間的持續時間
獲取 dateTimeOffset 和 … 之間的持續時間最大單位年數
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_diff\" disabled=\"true\" x=\"23\" y=\"248\"><field name=\"maxDurationUnit\">DurationUnit.Years</field><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此塊用於顯示兩個 DateTimeOffset 之間的差異。
參數:
- DateTimeOffset
- 條件:年(預設)/月/日/小時/分鐘/秒/毫秒
輸出: 期間
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDateTimeOffset\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"6\" _input_init=\"true\"></mutation><value name=\"year\"><shadow type=\"math_number\"><field name=\"NUM\">2020</field></shadow></value><value name=\"month\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><value name=\"day\"><shadow type=\"math_number\"><field name=\"NUM\">23</field></shadow></value><value name=\"hour\"><shadow type=\"math_number\"><field name=\"NUM\">5</field></shadow></value><value name=\"minute\"><shadow type=\"math_number\"><field name=\"NUM\">30</field></shadow></value><value name=\"second\"><shadow type=\"math_number\"><field name=\"NUM\">25</field></shadow></value><value name=\"offset\"><shadow type=\"text\"><field name=\"TEXT\">+00:00</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">currentTime</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_DateTimeOffset_diff\"><field name=\"maxDurationUnit\">DurationUnit.Minutes</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"other\"><block type=\"variables_get\"><field name=\"VAR\">currentTime</field></block></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}
輸出:
將 DateTimeOffset 與 Duration 相加
dateTimeOffset + 持續時間
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>duration</variable></variables><block type=\"DateTime_DateTimeOffset_arithmetic\" disabled=\"true\" x=\"195\" y=\"255\"><field name=\"op\">PlusMinus.Plus</field><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
檢查DateTimeOffsets是否相同
dateTimeOffset =
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>duration</variable></variables><block type=\"DateTime_DateTimeOffset_equals\" disabled=\"true\" x=\"-112\" y=\"248\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此塊用於檢查兩個 DateTimeOffsets 是否相同。
參數: DateTimeOffset
輸出: 布林值 (True/False)
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDateTimeOffset\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"0\" _input_init=\"false\"></mutation><value name=\"year\"><shadow type=\"math_number\"><field name=\"NUM\">2020</field></shadow></value></block></value><next><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"DateTime_DateTimeOffset_equals\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"other\"><block type=\"DateTime_parseDateTimeOffset\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">2020</field></shadow></value></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"Both of them are the same.\"</field></block></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"They are different.\"</field></block></value></block></statement></block></next></block></statement></block></xml>"}
輸出:
以毫秒為單位顯示 DateTimeOffset
dateTimeOffset 轉換為 Unix 時間(以毫秒為單位)
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_toUnixTimeMilliseconds\" disabled=\"true\" x=\"-23\" y=\"113\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此塊用於在 Unix 時間中顯示 DateTimeOffset。
參數: DateTimeOffset
輸出: 數
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>unixTime</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">unixTime</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_DateTimeOffset_toUnixTimeMilliseconds\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></next></block></statement></block></xml>"}
輸出:
以不同的時間格式顯示 DateTimeOffset
dateTimeOffset 獲取年份
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>unixTime</variable></variables><block type=\"DateTime_DateTimeOffset_get\" disabled=\"true\" x=\"-23\" y=\"158\"><field name=\"unit\">DateTimeUnit.Year</field><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
這個塊用於以不同的格式顯示時間。
參數:
- DateTimeOffset
- 條件: 年(預設)/ 月 / 日 / 小時 / 分鐘 / 秒 / 毫秒 / 偏移量 / 週年 / 週數 / 週日
輸出: 數
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"DateTime_DateTimeOffset_get\"><field name=\"unit\">DateTimeUnit.Year</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
檢查 DateTimeOffset 的類型是否正確
dateTimeOffset 有效
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_DateTimeOffset_isValid\" disabled=\"true\" x=\"-23\" y=\"338\"><value name=\"dateTimeOffset\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此塊用於檢查 DateTimeOffset 的數據類型是否正確。
參數: DateTimeOffset
輸出: 布林值 (True/False)
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"DateTime_DateTimeOffset_isValid\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"This is valid.\"</field></block></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"This is not valid.\"</field></block></value></block></statement></block></next></block></statement></block></xml>"}
輸出:
期間
將文本轉換為持續時間
解析…至持續時間
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable></variables><block type=\"DateTime_parseDuration\" disabled=\"true\" x=\"-23\" y=\"473\"><value name=\"s\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}
此塊用於將文本分析為期間。
參數: 字串
輸出: 期間
範例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">P20Y</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
將數位轉換為持續時間
創建 年持續時間
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"DateTime_createDuration\" disabled=\"true\" x=\"-23\" y=\"428\"><field name=\"unit\">DurationUnit.Years</field><value name=\"value\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}
此塊用於將數位生成為期間。
參數:
- 數字(預設值:0)
- 條件:年(預設)/月/天/小時/分鐘/秒/毫秒
輸出: 持續時間示例
代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDuration\"><field name=\"unit\">DurationUnit.Years</field><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">50</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
將字串轉換為持續時間
字串的持續時間
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"DateTime_Duration_toString\" disabled=\"true\" x=\"-23\" y=\"518\"><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於將期間分析為文字。
參數: 期間
輸出: 字串
示例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDuration\"><field name=\"unit\">DurationUnit.Years</field><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">50</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
製作相反的持續時間
否定持續時間
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"DateTime_Duration_negate\" disabled=\"true\" x=\"-23\" y=\"382\"><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於使持續時間變得相反。
參數: 期間
輸出: 持續時間示例
代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">dateTimeOffset</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDateTimeOffset\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"5\" _input_init=\"true\"></mutation><value name=\"year\"><shadow type=\"math_number\"><field name=\"NUM\">2020</field></shadow></value><value name=\"month\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><value name=\"day\"><shadow type=\"math_number\"><field name=\"NUM\">23</field></shadow></value><value name=\"hour\"><shadow type=\"math_number\"><field name=\"NUM\">5</field></shadow></value><value name=\"minute\"><shadow type=\"math_number\"><field name=\"NUM\">30</field></shadow></value><value name=\"second\"><shadow type=\"math_number\"><field name=\"NUM\">25</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">currentTime</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_now\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_DateTimeOffset_diff\"><field name=\"maxDurationUnit\">DurationUnit.Minutes</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"other\"><block type=\"variables_get\"><field name=\"VAR\">currentTime</field></block></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}
輸出:
添加兩個持續時間
持續時間 + …
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>duration</variable></variables><block type=\"DateTime_Duration_arithmetic\" disabled=\"true\" x=\"-23\" y=\"248\"><field name=\"op\">PlusMinus.Plus</field><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
更改持續時間單位
持續時間將最大單位更改為年數
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>duration</variable></variables><block type=\"DateTime_Duration_changeMaxUnit\" disabled=\"true\" x=\"-23\" y=\"338\"><field name=\"maxUnit\">DurationUnit.Years</field><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於將原始持續時間單位更改為新單位。
參數:
- 期間
- 條件:年(預設)/月/天/小時/分鐘/秒/毫秒
輸出: 持續時間示例
代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable><variable>unit</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDuration\"><field name=\"unit\">DurationUnit.Months</field><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">50</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">unit</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_Duration_changeMaxUnit\"><field name=\"maxUnit\">DurationUnit.Years</field><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">unit</field></block></value></block></value></block></next></block></next></block></statement></block></xml>"}
輸出:
檢查兩個持續時間是否相同
持續時間 = …
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable><variable>unit</variable></variables><block type=\"DateTime_Duration_equals\" disabled=\"true\" x=\"-23\" y=\"383\"><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於檢查兩個持續時間是否相同。
參數: 期間
輸出: 布爾值(True/False)
示例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDuration\"><field name=\"unit\">DurationUnit.Months</field><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">50</field></shadow></value></block></value><next><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"DateTime_Duration_equals\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value><value name=\"other\"><block type=\"DateTime_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">P50M</field></shadow></value></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">They are the same.</field></shadow></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">They are not the same.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}
輸出:
以不同單位顯示持續時間總數
持續時間獲取總年數
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"DateTime_Duration_getTotal\" disabled=\"true\" x=\"-23\" y=\"473\"><field name=\"unit\">DurationUnit.Years</field><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於以數位格式獲取持續時間。
參數:
- 期間
- 條件:年(預設)/月/天/小時/分鐘/秒/毫秒
輸出: 數位
示例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable><variable>total</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_createDuration\"><field name=\"unit\">DurationUnit.Years</field><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">50</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">total</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_Duration_getTotal\"><field name=\"unit\">DurationUnit.Years</field><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"variables_get\"><field name=\"VAR\">total</field></block></value></block></next></block></next></block></statement></block></xml>"}
輸出:
以數位格式顯示持續時間,帶單位
持續時間獲取 年
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable><variable>total</variable></variables><block type=\"DateTime_Duration_get\" disabled=\"true\" x=\"-23\" y=\"563\"><field name=\"unit\">DurationUnit.Years</field><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於以數位格式獲取指定的持續時間。
參數:
- 期間
- 條件:年(預設)/月/天/小時/分鐘/秒/毫秒
輸出: 數位
示例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">P4Y3M15DT23H20M30S</field></shadow></value></block></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"DateTime_Duration_get\"><field name=\"unit\">DurationUnit.Years</field><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value></block></next></block></statement></block></xml>"}
輸出:
檢查持續時間的數據類型是否正確
持續時間有效
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"DateTime_Duration_isValid\" disabled=\"true\" x=\"-23\" y=\"608\"><value name=\"duration\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此塊用於檢查持續時間的數據類型是否正確。
參數: 期間
輸出: 布爾值(True/False)
示例代碼:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">duration</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">P4Y3M15DT23H20M30S</field></shadow></value></block></value><next><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"DateTime_Duration_isValid\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">This is valid.</field></shadow></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">This is not valid.</field></shadow></value></block></statement></block></next></block></statement></block></xml>"}
輸出:
目録