简体   繁体   English

如何使用 PowerShell 将当前日期计算为自 1601-01-01 以来的毫秒数

[英]How to calculate current date as number of milliseconds since 1601-01-01 with PowerShell

How to calculate number of miliseconds since 1601-01-01 for today's date with windows PowerShell script?如何使用 Windows PowerShell 脚本计算今天日期自 1601-01-01 以来的毫秒数? I need this to build correct LDAP queries.我需要它来构建正确的 LDAP 查询。

A DateTime structure contains method ToFileTime . DateTime结构包含方法ToFileTime As per documentation ,根据文档

A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 AD (CE) Coordinated Universal Time (UTC). Windows 文件时间是一个 64 位值,表示自公元 1601 年 1 月 1 日午夜 12:00 起经过的 100 纳秒间隔数(公元 1601 年 1 月 1 日)协调世界时 (UTC)。

Thus, going from ns (10e-9) to ms (10e-3) is simple arithmetics.因此,从 ns (10e-9) 到 ms (10e-3) 是简单的算术运算。 Just mind that the counter counts 100 ns blocks, not 1 ns blocks.请注意,计数器计数 100 ns 块,而不是 1 ns 块。 The value is stored as an Int64, so no type conversion is needed.该值存储为 Int64,因此不需要类型转换。 Like so,像这样,

PS C:\> (Get-Date).ToFileTime()
130142949169114886
PS C:\> (Get-Date).ToFileTime().GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int64                                    System.ValueType

Full agree with @vonPryz answer.完全同意@vonPryz 的回答。 Just for fun You can find the number of 100-nanosecond in the tick property of the Powershell System.DateTime .只是为了好玩您可以在 Powershell System.DateTimetick属性中找到 100 纳秒的数量。 But this tick is not from "01/01/1600" but from ([datetime]::MinValue) "01/01/0001".但是这个刻度不是来自“01/01/1600”而是来自([datetime]::MinValue)“01/01/0001”。

try :尝试 :

$a = ([datetime]::Now).Ticks  - ([datetime]("01/01/1600 12:00")).Ticks
[datetime]::FromFileTimeUtc($a)

It will be correct:这将是正确的:

(Get-Date).ToFileTime()/10000

It's scary to even think what will happen to us if the above simple solution (Get-Date).ToFileTime() gives an error of 10,000 times如果上面的简单解决方案 (Get-Date).ToFileTime() 给出 10,000 次的错误,甚至想到我们会发生什么都令人恐惧

4205233 years. 4205233 年。 It's terrible它是可怕的

$a = ([datetime]::Now).Ticks
$secTimer=1
Start-Sleep -Seconds $secTimer
$b = ([datetime]::Now).Ticks
$c=$b-$a
'ticks={0} == {1} sec and {2} ticks' -f $c,[int](Get-Date  $c -Format "ss"),[int](Get-Date  $c -Format "fffffff")
$TicksPerSec = $c/$secTimer
'ticks per second = {0}' -f ($c/$secTimer)
echo "`n"
$Year=1601;$Month=1;$date=1;$hour=0;$minutes=0;$Seconds=0;$mSeconds=0;
$Ticks1601 = New-Object DateTime $Year, $Month, $date, $hour, $minutes, $Seconds, $mSeconds
'Ticks on Jan 1 1601 00:00:00   =  {0}' -f $Ticks1601.Ticks
$TicksNow = ([datetime]::Now).Ticks
$time=$TicksNow-$Ticks1601
'after Jan 1 1601 00:00:00'
'  milliseconds {0}' -f ($time.Ticks/$TicksPerSec*1000)
$seconds=$time.Ticks/$TicksPerSec
'  seconds =    {0}' -f $seconds
$min=$seconds/60
'  minutes =    {0}' -f $min
$hours=$min/60
'  hours =      {0}' -f $hours
$days=$hours/24
'  days =        {0}' -f $days
$years=$days/364.75
'  years =      {0}' -f $years

echo "`nand`n"
$ms=(Get-Date).ToFileTime()
'simple ToFileTime() after Jan 1 1601 00:00:00'
'  milliseconds {0}' -f $ms
$years=$ms/1000/60/60/24/364.75
'  years =      {0}' -f $years
'???'
echo "`n"
$Year=1;$Month=1;$date=1;$hour=0;$minutes=0;$Seconds=0;$mSeconds=0;
$time = New-Object DateTime $Year, $Month, $date, $hour, $minutes, $Seconds, $mSeconds
'Ticks on Jan 1 0001 00:00:00   =  {0}' -f $time.Ticks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 1601 / 01/01的lastLogonTimeStamp属性 - 1601/01/01 of lastLogonTimeStamp attribute Powershell | 如何计算当前日期 -5 天到纪元时间? - Powershell | How to calculate current date -5 days to Epoch time? 如何在powershell中将毫秒转换为日期和时间? - How to convert milliseconds to date and time in powershell? 在 api 版本 2018-01-01- Azure Powershell 的命名空间 Microsoft.storage 中找不到资源类型 - The resource type could not be found in the namespace Microsoft.storage for api version 2018-01-01- Azure Powershell Powershell 7 和 IE 自动化错误 0x800A01B6 - Powershell 7 and IE automation Error 0x800A01B6 在Powershell中计算特定日期 - Calculate specific Date in powershell 使用PowerShell计算日期 - Calculate Date with PowerShell 尝试在IE 11中使用Powershell中的getElementById,来自HRESULT的错误异常:0x800A01B6 - Trying to use getElementById in Powershell with IE 11, error Exception from HRESULT: 0x800A01B6 尝试向 Facebook 输入登录信息时,PowerShell 中出现“来自 HRESULT 的异常:0X800A01B6”错误 - 'Exception from HRESULT: 0X800A01B6' error in PowerShell when trying to enter login information to Facebook 如何使用 Powershell 列出自昨天以来磁盘上的名称、上次写入日期和文件大小? - How can I use Powershell to list the names, last write date and file sizes on a disk SINCE YESTERDAY?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM