简体   繁体   English

在PowerShell中将其他区域日期时间格式转换为本地日期时间格式

[英]Converting other zone date time format to local date-time format in PowerShell

I am trying to perform date-time comparison. 我正在尝试执行日期时间比较。

Datetime in log file- Tue Aug 4 17:05:41 2015 日志文件中的日期时间- 2015年8月4日星期二17:05:41

Local system date time from get-date command - 来自get-date命令的本地系统日期时间-

6. elokuuta 2015 10:18:47 6. elokuuta 2015 10:18:47

It's 6 Aug 2015 10:18:47 after I translated it on Google Translate. 我在Google翻译上翻译后的时间是2015年8月6日10:18:47。

How do I convert this date/time format, so I can perform comparison? 如何转换此日期/时间格式,以便进行比较?

I tried multiple things and options, but no luck. 我尝试了多种方法和选项,但是没有运气。

My script so far: 到目前为止,我的脚本是:

# To get today's date
$Today=[datetime]::Today
# Output is 6. elokuuta 2015 0:00:00

$log_date = "Tue Aug  4 17:05:41 2015"

I read somewhere that by using new-object system.globalization.datetimeformatinfo it can be achieved, but I don't know how to change the current date-time with this. 我在某处读到可以通过使用new-object system.globalization.datetimeformatinfo来实现,但我不知道如何用此方法更改当前日期时间。 https://technet.microsoft.com/en-us/library/ff730960.aspx https://technet.microsoft.com/zh-CN/library/ff730960.aspx

[DateTime] objects do not have a format. [DateTime]对象没有格式。 It just a number of ticks (0.0000001 second) passed since some point in time (01/01/0001 00:00:00.0000000). 从某个时间点(01/01/0001 00:00:00.0000000)开始经过的ticks (0.0000001秒)。 The format is only needed to save a [DateTime] object as a string or to display it to the user, since 635744160000000000 ticks is not really understandable to a user. 该格式仅需要将[DateTime]对象另存为字符串或将其显示给用户,因为635744160000000000 ticks对于用户而言确实不是很容易理解。

All you need is to parse the string to get a [DateTime] object: 您所需要做的就是解析字符串以获得[DateTime]对象:

$log_date = [datetime]::ParseExact('Tue Aug  4 17:05:41 2015','ddd MMM d HH:mm:ss yyyy',[cultureinfo]::InvariantCulture,'AllowInnerWhite')

Now as you have two [DateTime] objects you can compare them: 现在,有了两个[DateTime]对象,就可以对其进行比较:

$Today -eq $log_date
$Today -lt $log_date
$Today -gt $log_date

Or find a difference between them: 或者找到它们之间的区别:

$Today - $log_date

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM