Powershell將信息存儲在對象中,每個對象都會有一個具體的類型,簡單的文本會以System.String類型存儲,日期會以System.DateTime類型存儲。任何.NET對象都可以通過GetType()方法返回它的類型,該類型中有一個FullName屬性,可以查看類型的完整名稱。
代碼如下:
PS C:Powershell> $date=get-date
PS C:Powershell> $date
2012年1月11日 15:19:49
PS C:Powershell> $date.GetType().FullName
System.DateTime
每一個類型都 可以包含一些靜態的方法,可以通過方括號和類型名稱得到類型對象本身,然后通過Get-Memeber命令查看該類型支持的所有靜態方法。
代碼如下:
PS C:Powershell> [System.DateTime] | Get-Member -static -memberType *Method
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Compare Method static int Compare(System.DateTime t1, System.Dat...
DaysInMonth Method static int DaysInMonth(int year, int month)
Equals Method static bool Equals(System.DateTime t1, System.Dat...
FromBinary Method static System.DateTime FromBinary(long dateData)
FromFileTime Method static System.DateTime FromFileTime(long fileTime)
FromFileTimeUtc Method static System.DateTime FromFileTimeUtc(long fileT...
FromOADate Method static System.DateTime FromOADate(double d)
IsLeapYear Method static bool IsLeapYear(int year)
Parse Method static System.DateTime Parse(string s), static Sy...
ParseExact Method static System.DateTime ParseExact(string s, strin...
ReferenceEquales Method static bool ReferenceEquals(System.Object objA, S...
SpecifyKind Method static System.DateTime SpecifyKind(System.DateTim...
TryParse Method static bool TryParse(string s, System.DateTime&, ...
TryParseExact Method static bool TryParseExact(string s, string format...
System.DateTime類支持的靜態方法非常實用
使用Parse方法將一個字符串轉換成DateTime類:
代碼如下:
PS C:Powershell> [System.DateTime]::Parse("2012-10-13 23:42:55")
2012年10月13日 23:42:55
新聞熱點
疑難解答