我想使用 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 小时格式 -
使用
make_datetime()
或使用日期时间/时间跨度算法从中创建datetime
值。
例如:
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)