You need to enable JavaScript to run this app. 如何在PowerShell中获取Windows 32位格式的时间/日期值
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell中获取Windows 32位格式的时间/日期值

可用PowerShell实现方案

你可以直接使用以下代码完成转换,输出结果完全符合Windows C Runtime 32位时间/日期编码规范:

完整可运行代码

# 读取当前本地时间
$currentTime = Get-Date
# 计算16位日期段:年偏移(基准1980年,占7位) + 月(占4位) + 日(占5位)
$dateSegment = (($currentTime.Year - 1980) -shl 9) -bor ($currentTime.Month -shl 5) -bor $currentTime.Day
# 计算16位时间段:时(占5位) + 分(占6位) + 秒/2(占5位,格式原生精度为2秒)
$timeSegment = ($currentTime.Hour -shl 11) -bor ($currentTime.Minute -shl 5) -bor ([Math]::Floor($currentTime.Second / 2))
# 合并为32位无符号整数结果
$crt32Value = [uint32](($dateSegment -shl 16) -bor $timeSegment)

# 输出两种常用格式的结果
Write-Host "十进制编码结果:$crt32Value"
Write-Host "十六进制编码结果:0x$($crt32Value.ToString('X8'))"

一行简化版命令

如果需要直接运行的短命令,可以使用以下版本:

$t=Get-Date;[uint32](((($t.Year-1980)-shl9)-bor($t.Month-shl5)-bor$t.Day)-shl16)-bor(($t.Hour-shl11)-bor($t.Minute-shl5)-bor([Math]::Floor($t.Second/2)))

注意事项

  • 该编码格式的时间精度为2秒,转换时会自动将原始秒数除以2向下取整,属于规范原生设计
  • 日期基准为1980年1月1日,仅支持1980年至2107年之间的时间转换
  • 输出结果和C Runtime库内置_dosdate_t等结构返回的编码完全兼容

内容的提问来源于stack exchange,提问作者ivvi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 23:24:04