Azure Kusto :: 将 12 小时格式的时间转换为 24 小时格式

Azure Kusto :: Convert Time in 12 Hour Format to 24 Hour Format

我想使用 Azure Kusto 语言将以下 12 小时制时间格式转换为 24 小时制时间格式。我希望输出从 07:00:00 AM 转换为 07:00:00 和 07:00:00 PM 转换为 19:00:00。执行以下查询不会导致正确的输出,确定我遗漏了一些东西。有谁能帮忙吗

datatable (Date:string, Event:string)
    ['07:00:00 AM', "Morning",
     '07:00:00 PM', "Evening"]
     | extend val = todatetime(Date), val2 = format_datetime(todatetime(strcat('1900-01-01, ',Date)),'HH:mm:ss')

a. 如果你想创建一个 datetime 值,你可以尝试这样的事情:

  • 解析字符串中的部分(小时、分钟...)。

  • hours部分调整为24小时格式

  • 从中创建一个 datetime 值,使用 make_datetime(),或使用 datetime / timespan arithmetic

例如:

datatable (TimeOfDay:string, Event:string)
[
    '07:00:01 AM', "Morning",
    '07:01:23 PM', "Evening"
]
| parse TimeOfDay with hours:int ":" minutes:int ":" seconds:int " " am_pm
| extend hours = case(am_pm == "AM" and hours == 12, hours - 12,
                      am_pm == "AM", hours,
                      hours == 12, hours,
                      hours + 12)
| project dt = make_datetime(1970, 1, 1, hours, minutes, seconds) // just used 1970-01-01 for the example

b. 否则,如果你只想创建一个字符串值,你可以做一些类似的事情:

datatable (TimeOfDay:string, Event:string)
[
    '07:00:01 AM', "Morning",
    '07:01:23 PM', "Evening"
]
| parse TimeOfDay with hours:int ":" minutes_seconds " " am_pm
| extend hours = case(am_pm == "AM" and hours == 12, tostring(hours),
                      am_pm == "AM", strcat("0", hours),
                      hours == 12, tostring(hours),
                      tostring(hours + 12))
| project str = strcat(hours, ":", minutes_seconds)