本文档介绍涉及 DateTimeOffset 和 Duration 的操作和转换,包括格式设置、计算、类型检查以及文本、数字和各种单位之间的转换。
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
- 条件:年(默认)/月/日/小时/分钟/秒/毫秒/偏移量/WeekYear/WeekNumber/WeekDay
输出: 数
范例代码:
{"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>"}
此块用于将文本分析为 Duration。
参数: 字符串
输出: 期间
范例代码:
{"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>"}
此块用于将数字生成为 Duration。
参数:
- 数字(预设值: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>"}
此块用于将 Duration 分析为文本。
参数: 期间
输出: 字符串
示例代码:
{"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>"}
输出:
目录