在Powershell中创建其他时区的datetime对象 [英] Create datetime object of other timezone in Powershell

查看:718
本文介绍了在Powershell中创建其他时区的datetime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试在考虑DST的同时,将特定时区的日期时间转换为其他时区。例如

Basically I am trying to find a way to convert datetime of a particular timezone to other timezone while taking DST into consideration too. e.g.

中太平洋标准时间的时间是什么时候,比如2012/9/29 9:00 AM在东京标准时间?

What is the time in "Central Pacific Standard Time" when it is, say, 2012/9/29 9:00AM in "Tokyo Standard Time" ?

我在Internet上找到了一些解决方案,将本地机器时间转换为其他时区。

I found some solutions on the Internet to convert local machine time to other timezone.

$ToTimeZoneObj = [system.timezoneinfo]::GetSystemTimeZones() | Where-Object {$_.id -eq $ToTimeZone}
$TargetZoneTime = [system.timezoneinfo]::ConvertTime($datetime, $ToTimeZoneObj)

我正在考虑如果我可以创建与本地机器不同的时区的datetime对象,那么我可以使用我发现的解决方案,还是有其他方法我需要什么?

I am thinking if I can create a datetime object of a timezone different from the local machine, I can then use the solutions I found, or will there be other ways to do what I need?

谢谢

推荐答案

我:

$cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
$csttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $cstzone)

然后您可以像datetime对象那样操纵$ csttime变量:

You can then manipulate the $csttime variable just like a datetime object:

Get-Date $csttime.AddHours(-1) -f "MM\dd\yyyy HH:mm:ss"

参考文献:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc(v = vs.110).aspx
http://technet.microsoft.com/en-us/library/ee692801.aspx

这篇关于在Powershell中创建其他时区的datetime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆