在Azure Data Explorer中将DateTime转换为Epoch时间戳
在Azure Data Explorer(ADX)中将日期时间转换为Epoch时间戳
你可以通过以下两种实用方式实现日期时间到Epoch时间戳(秒级)的转换:
方法1:通过时间差计算实现
Epoch时间的起点是1970-01-01T00:00:00Z,你可以用datetime_diff函数计算目标日期时间与该起点的秒数差,直接得到结果:
// 先将字符串日期转换为datetime类型,再计算秒数差 let target_datetime = datetime(16 May 2023 10:15:49); print epoch_timestamp = datetime_diff('second', datetime(1970-01-01 00:00:00), target_datetime)
执行后会输出你需要的1684232149。
方法2:使用内置转换函数(推荐)
ADX提供了直接转换的内置函数to_unixtime(),它会返回指定datetime对应的Epoch秒数,写法更简洁:
let target_datetime = datetime(16 May 2023 10:15:49); print epoch_timestamp = to_unixtime(target_datetime)
关键注意事项
- 若你的日期时间是本地时区,需先转换为UTC再计算,避免结果偏差:
// 示例:本地时间转UTC后再生成Epoch let local_datetime = todatetime("16 May 2023 10:15:49"); let utc_datetime = convert_timezone(local_datetime, "UTC"); print epoch_timestamp = to_unixtime(utc_datetime) - 如需毫秒级Epoch时间戳,方法1可将单位改为
millisecond,方法2可使用to_unixtime_milliseconds()函数。
内容的提问来源于stack exchange,提问作者Pathrudu
相关产品推荐
相关产品推荐

