本文档提供了有关处理 DateTimeOffset 和 Duration 的全面详细信息,包括 DateTime 数据的转换、格式设置、计算、修改和验证。
DateTimeOffset
将文本转换为 DateTimeOffset 格式
解析。。。到 DateTimeOffset
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_parseDateTimeOffset\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow></value></block></xml>"}
此块用于将文本分析到 DateTimeOffset 中。
参数: 字符串
输出: DateTimeOffset
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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\">2020-08-16</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
生成具有不同单位的 DateTimeOffset
使用 2020 年创建 DateTimeOffset
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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></xml>"}
此块用于随时间生成新的 DateTimeOffset。
参数: 数字(默认值:2020)
输出: DateTimeOffset
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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\">5</field></shadow></value><value name=\"day\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><value name=\"hour\"><shadow type=\"math_number\"><field name=\"NUM\">23</field></shadow></value><value name=\"minute\"><shadow type=\"math_number\"><field name=\"NUM\">22</field></shadow></value><value name=\"second\"><shadow type=\"math_number\"><field name=\"NUM\">50</field></shadow></value><value name=\"offset\"><shadow type=\"text\"><field name=\"TEXT\">+25:35</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
使用 Unix 时间生成 DateTimeOffset
从 Unix 时间创建 DateTimeOffset,以毫秒为 0
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_fromUnixTimeMilliseconds\"><value name=\"milliseconds\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}
此块用于使用 Unix 时间(以毫秒为单位)生成新的 DateTimeOffset。
参数: 数字(默认值:0)
输出: DateTimeOffset
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
使用当前时间显示 DateTimeOffset
使用当前时间创建 DateTimeOffset
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_now\"/></xml>"}
此块用于使用当前时间生成新的 DateTimeOffset。
参数: N/A
输出: DateTimeOffset
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
将新偏移量与 DateTimeOffset 合并
将 dateTimeOffset 转换为偏移量 +00:00
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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\">+00:00</field></shadow></value></block></xml>"}
此块用于将 DateTimeOffset 与新的偏移量组合在一起。
参数:
- DateTimeOffset
- 字符串
输出: DateTimeOffset
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
修改偏移量
将 dateTimeOffset 的偏移量设置为 +00:00
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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\">+00:00</field></shadow></value></block></xml>"}
此块用于设置与原始 DateTimeOffset 的新偏移量。
参数:
- DateTimeOffset
- 字符串
输出: DateTimeOffset
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
将 DateTimeOffset 的格式更改为文本格式
dateTimeOffset 转换为格式为 yyyy-MM-dd HH:mm:ss ZZ 的字符串
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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></xml>"}
此块用于将 DateTimeOffset 更改为特定的字符串格式。
参数:
- DateTimeOffset
- 字符串
输出: 字符串
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
将 DateTimeOffset 转换为文本格式
dateTimeOffset 转换为字符串
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此块用于将 DateTimeOffset 转换为字符串。
参数: DateTimeOffset
输出: 字符串
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
将 DateTimeOffset 与 Duration 相加
dateTimeOffset + 持续时间
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_DateTimeOffset_arithmetic\"><field name=\"op\">PlusMinus.Plus</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此块用于添加日期、时间偏移量和持续时间。
参数:
- DateTimeOffset
- 期间
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>duration</variable><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>calculate</variable><variable>layout</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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_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\">2021</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\">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=\"variables_set\"><field name=\"VAR\">calculate</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_DateTimeOffset_arithmetic\"><field name=\"op\">PlusMinus.Minus</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">calculate</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
当它们相加时,输出:
当将它们相减时,输出:
计算两个 DateTimeOffsets 之间的持续时间
获取 dateTimeOffset 和 … 之间的持续时间最大单位年数
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_DateTimeOffset_diff\"><field name=\"maxDurationUnit\">DurationUnit.Years</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此块用于显示两个 DateTimeOffset 之间的差异。
参数:
- DateTimeOffset
- 条件:年(默认)/月/日/小时/分钟/秒/毫秒
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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.Years</field><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">currentTime</field></block></value><value name=\"other\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
检查DateTimeOffsets是否相同
dateTimeOffset = …
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_DateTimeOffset_equals\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此块用于检查两个 DateTimeOffsets 是否相同。
参数: DateTimeOffset
输出: 布尔值 (True/False)
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">Both of them are the same.</field></shadow></value></block></value></block></statement><statement name=\"ELSE\"><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">They are different</field></shadow></value></block></value></block></statement></block></next></block></next></block></next></block></statement></block></xml>"}
如果为 true,则输出:
当为 false 时,输出:
以毫秒为单位显示 DateTimeOffset
dateTimeOffset 转换为 Unix 时间(以毫秒为单位)
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_DateTimeOffset_toUnixTimeMilliseconds\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此块用于在 Unix 时间中显示 DateTimeOffset。
参数: DateTimeOffset
输出: 数
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">unixTime.toString()</field></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
以不同的时间格式显示 DateTimeOffset
dateTimeOffset 获取年份
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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></xml>"}
此块用于以不同的格式显示时间。
参数:
- DateTimeOffset
- 条件:年(默认)/月/日/小时/分钟/秒/毫秒/偏移量/WeekYear/WeekNumber/WeekDay
输出: 数
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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\">time</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><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><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">time</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
当它设置为 Year 时,输出:
当它设置为 Month 时,输出:
当它设置为 Day 时,输出:
当它设置为 Hour 时,输出:
当它设置为 Minute 时,输出:
当它设置为 Second 时,输出:
当它设置为毫秒时,输出:
当它设置为 Offset 时,输出:
当它设置为 WeekYear 时,输出:
当它设置为 WeekNumber 时,输出:
当它设置为 WeekDay 时,输出:
检查 DateTimeOffset 的类型是否正确
dateTimeOffset 有效
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_DateTimeOffset_isValid\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">dateTimeOffset</field></block></value></block></xml>"}
此块用于检查 DateTimeOffset 的数据类型是否正确。
参数: DateTimeOffset
输出: 布尔值 (True/False)
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">This is valid.</field></shadow></value></block></value></block></statement><statement name=\"ELSE\"><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">This is not valid.</field></shadow></value></block></value></block></statement></block></next></block></next></block></next></block></statement></block></xml>"}
当它处于正确的数据类型时,输出:
当它的数据类型错误时,输出:
期间
将文本转换为持续时间
解析。。。至 持续时间
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow></value></block></xml>"}
此块用于将文本分析为 Duration。
参数: 字符串
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">P20Y</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
将数字转换为持续时间
创建持续时间为 0 年
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_createDuration\"><field name=\"unit\">DurationUnit.Years</field><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}
此块用于将数字生成为 Duration。
参数:
- 数字(预设值:0)
- 条件:年(默认)/月/天/小时/分钟/秒/毫秒
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
当它设置为“年”时,输出:
当它设置为 Months 时,输出:
当它设置为 Days 时,输出:
当它设置为 Hours 时,输出:
当它设置为分钟时,输出:
当它设置为 Seconds 时,输出:
当它设置为毫秒时,输出:
将字符串转换为持续时间
字符串的持续时间
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此块用于将 Duration 分析为文本。
参数: 期间
输出: 字符串
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
制作相反的持续时间
否定持续时间
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_Duration_negate\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此块用于使持续时间变得相反。
参数: 期间
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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\">negation</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_Duration_negate\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_Duration_toString\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">negation</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
输出:
添加两个持续时间
杜拉顿 + …
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_Duration_arithmetic\"><field name=\"op\">PlusMinus.Plus</field><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此块用于将两个持续时间相加。
参数:
- 期间
- 条件:+(默认)/-
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>duration</variable><variable>dateTimeOffset</variable><variable>currentTime</variable><variable>calculate</variable><variable>layout</variable><variable>sum</variable><variable>secondDuration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><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=\"variables_set\"><field name=\"VAR\">secondDuration</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\">PT-769640M-40.604S</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">sum</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"DateTime_Duration_arithmetic\"><field name=\"op\">PlusMinus.Plus</field><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value><value name=\"other\"><block type=\"variables_get\"><field name=\"VAR\">secondDuration</field></block></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
将持续时间相加时,输出:
减去持续时间时,输出:
更改持续时间单位
持续时间将最大单位更改为年数
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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></xml>"}
此块用于将原始持续时间单位更改为新单位。
参数:
- 期间
- 条件:年(默认)/月/天/小时/分钟/秒/毫秒
输出: 期间
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><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></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
当它设置为“年”时,输出:
当它设置为 Months 时,输出:
当它设置为 Days 时,输出:
当它设置为 Hours 时,输出:
当它设置为分钟时,输出:
当它设置为 Seconds 时,输出:
当它设置为毫秒时,输出:
检查两个持续时间是否相同
持续时间 = …
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_Duration_equals\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此块用于检查两个持续时间是否相同。
参数: 期间
输出: 布尔值 (True/False)
示例代码:
当它们相同时,输出:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>duration</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">They are the same.</field></shadow></value></block></value></block></statement><statement name=\"ELSE\"><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">They are not the same.</field></shadow></value></block></value></block></statement></block></next></block></next></block></next></block></statement></block></xml>"}
当它们不同时,输出:
以不同单位显示持续时间总数
持续时间获取总年数
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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></xml>"}
此块用于以数字格式获取持续时间。
参数:
- 期间
- 条件:年(默认)/月/天/小时/分钟/秒/毫秒
输出: 数
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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\">sum</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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"Util_convertToText\"><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">sum</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
当它设置为“年”时,输出:
当它设置为 Months 时,输出:
当它设置为 Days 时,输出:
当它设置为 Hours 时,输出:
当它设置为分钟时,输出:
当它设置为 Seconds 时,输出:
当它设置为毫秒时,输出:
以数字格式显示持续时间,带单位
持续时间 获取 年
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><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></xml>"}
此块用于以数字格式获取指定的持续时间。
参数:
- 期间
- 条件:年(默认)/月/天/小时/分钟/秒/毫秒
输出: 数
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_parseDuration\"><value name=\"s\"><shadow type=\"text\"><field name=\"TEXT\">P4Y3M15DT23H20M30S</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">date</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><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><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"Util_convertToText\"><value name=\"value\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">date</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}
当它设置为“年”时,输出:
当它设置为 Months 时,输出:
当它设置为 Days 时,输出:
当它设置为 Hours 时,输出:
当它设置为分钟时,输出:
当它设置为 Seconds 时,输出:
当它设置为毫秒时,输出:
检查持续时间的数据类型是否正确
持续时间有效
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"DateTime_Duration_isValid\"><value name=\"duration\"><block type=\"variables_get\"><field name=\"VAR\">duration</field></block></value></block></xml>"}
此块用于检查持续时间的数据类型是否正确。
参数: 期间
输出: 布尔值 (True/False)
示例代码:
当它处于正确的类型时,输出:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></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_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=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It is valid.</field></shadow></value></block></value></block></statement><statement name=\"ELSE\"><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">It is not valid.</field></shadow></value></block></value></block></statement></block></next></block></next></block></next></block></statement></block></xml>"}
当它处于错误的类型时,输出:
目录