5

I've tried a lot of different ways and I can't seem to get it right.

Here is the code of what I have tried so far...

[String]$dateValue = '20161212'
[String]$dateStamp = $dateValue -f (Get-Date)
[String]$dateStamp2 = ([datetime]::parseexact($dateValue, "yyyyMMdd", [System.Globalization.CultureInfo]::InvariantCulture)).Date
[String]$dateStamp3 = ([datetime]::FromFileTime($dateValue)).ToString('g')

Write-Host '$dateStamp  = ' $dateStamp
Write-Host '$dateStamp2 = ' $dateStamp2
Write-Host '$dateStamp3 = ' $dateStamp3

Current Code Output

$dateStamp = 20161212
$dateStamp2 = 12/12/2016 00:00:00
$dateStamp3 = 12/31/1600 5:00 PM

Desired Code Output

$dateStamp = 12/12/2016

Any Ideas?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • 2
    `$dateStamp2` seems correct. How do you intend to come up with 3:00 PM when that information isn't in the source string? – Ryan Bemrose Apr 12 '16 at 01:40
  • 2
    $dateStamp3` is totally wrong, because you're not starting with a FileTime, and therefore you can't get what you want from `FromFileTime` (which should be clear by reading the method name). Where does this imaginary time come from that you want to retrieve? You never assigned one when you initialize `$dateValue`; do you expect 3:00PM to just magically appear? – Ken White Apr 12 '16 at 01:42
  • TBH, I wasn't thinking about how to handle the hh:mm. I guess the actual time doesn't matter since it's not provided, but the format should still at least be '**/**/****'. I updated the question to exclude the HH:MM. – Fiddle Freak Apr 12 '16 at 01:47

2 Answers2

10

Once you have a datetime object it's easy to convert it to whatever string format you need. You are so close with your second attempt. Adding ToString allows you to specify a string format.

([datetime]::parseexact($dateValue, "yyyyMMdd", [System.Globalization.CultureInfo]::InvariantCulture)).ToString("dd/MM/yyyy")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick
  • 1,783
  • 1
  • 15
  • 18
1

Given that you have a culture-invariant string as your input and that you want a fixed output format, you may as well perform string parsing, without the need to convert to an intermediate [datetime] instance:

> '20161213' -replace '\d{2}(\d{2})(\d{2})(\d{2})', '$2/$3/$1'
12/13/16

Note that I've changed the day to be different from the month to better highlight the reformatting that takes place.

Generally, though, the [datetime]-based method demonstrated in Nick's helpful answer gives you the most flexibility.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Good suggestion. However I'm not too comfortable doing regex when dealing with date comparisons, and would prefer the [datetime] library be used. But I certainly thank you for the valid thought (I never know when something like this may be useful). :) – Fiddle Freak Apr 12 '16 at 02:22
  • @FiddleFreak: My pleasure; yes, `[datetime]` is generally preferable, especially when it comes to date comparisons/calculations/culture-awareness. – mklement0 Apr 12 '16 at 02:25