-1

At my work, I'm getting some dates from AD attributes that were set by other scripts in the company, but to a unfriendly format I would say.

The Strings are the following: 201710191528 Which Stand to: YYYYMMDDHHmm

I want to convert this to a Date Object but so far without success.

I though about using StringBuilder to manipulate the String first to a format I can use on Get-Date() doing the following:

$strB = New-Object System.Text.StringBuilder
$strB.Append("201710191528")
$strB.Insert(10,":")
$strB.Insert(8," ")
$strB.Insert(6,"-")
$strB.Insert(4,"-")

And this works. StringBuilder will have "2017-10-19 15:28" which is perfect for Get-Date where I can just use it will work.

Now, I will be processing thousands of strings like this regularly. Is there something more efficient? I couldn't to get it to work with string formats.

Thank you very much.

0

2 Answers 2

0
[datetime]::ParseExact('201710191528','yyyyMMddHHmm',$null)

http://winpowershell.blogspot.in/2006/09/systemdatetime-parseexact.html

Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. Exactly what I was looking for. Thank you very much ;)
0

201710191528 will be converted in 2017-10-19 15:28 :

$strDate = "201710191528"
Get-Date([datetime]::ParseExact($strDate,'yyyyMMddHHmm',$null)) -Format "yyyy-MM-dd HH:mm"

1 Comment

This is redundant. The Object type DateTime is already being cast, so doing the ParseExact in this case will return a DateTime Object. No need for Get-Date. Thank you for the suggestion though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.