在powershell中将奇数格式转换为[datetime]

时间:2016-08-22 20:23:08

标签: .net powershell datetime

我正在写一些东西来获取某个程序的安装日期,但我似乎无法将返回的数字转换为奇怪的日期格式(yyyyMMdd)。我已经尝试将其转换为// // Created by Joe Ruth on 1/10/16. // Copyright © 2016 Joe Ruth. All rights reserved. // #import "SaveObjectUIDocument.h" #import "SaveObject.h" #define kArchiveKey @"SaveObjectXMLData" @interface SaveObjectUIDocument () @property (nonatomic, strong) NSData * data; @end @implementation SaveObjectUIDocument; @synthesize SaveObject = _SaveObject; - (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError { if ([contents length] > 0) { NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:contents]; self.SaveObject = [unarchiver decodeObjectForKey:kArchiveKey]; [unarchiver finishDecoding];} else { self.SaveObject = [[SaveObject alloc] initWithData:nil];} return YES; } - (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError { NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:self.SaveObject forKey:kArchiveKey]; [archiver finishEncoding]; return data; } - (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted { NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo); [super handleError:error userInteractionPermitted:userInteractionPermitted]; } @end ,但这会返回以下错误。

这可能是一个简单的解决方法,但这是我尚未遇到的问题。有人可以帮忙吗?

提前致谢!

[datetime]

$test = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq 'exampleProgram'} | select installdate

2 个答案:

答案 0 :(得分:2)

鉴于你的日期" 20160628" 尝试:

[datetime]::ParseExact($Date,"yyyyMMdd",$null)

你得到:

Tuesday, June 28, 2016 12:00:00 AM

答案 1 :(得分:2)

您需要使用不变的cultureInfo和自定义格式将日期解析为变量:

$DateTimeVariable = [DateTime]::ParseExact("20160628", "yyyyMMdd", System.Globalization.CultureInfo]::InvariantCulture)

$DateTimeVariable = [DateTime]::ParseExact($test.installdate, "yyyyMMdd", System.Globalization.CultureInfo]::InvariantCulture)