locked
Need help converting Office_2018-04-02T00-04-02 to 2018-04-02 00-04-02 in powershell RRS feed

  • Question

  • Hi All,

    I kinda need help converting the value from Office_2018-04-02T00-04-02 to 2018-04-02 00-04-02.

    Once i get the help here I'll put in my foreach loop which will do the same for multiple places as I have multiple places where I need to so the same conversion.



    Tuesday, April 3, 2018 5:50 PM

All replies

  • Use Split and the [datetime] accelerator to recover the date as a local date time. 

    $time = ('Office_2018-04-02T00-04-02' -split '_')[1]
    $time2 = $time -split 'T'
    $dateparts = $time2[0] -split '-'
    $timeparts = $time2[1] -split '-'

    "$*parts" contain the arrays of date and time bits that can be converted easily.


    \_(ツ)_/

    Tuesday, April 3, 2018 6:31 PM
  • Thanks JRV.

    I see that below 1st 2 lines is nicely splitting the file name to timestamp. However the output is coming in 2 line.

    $time = ('Office_2018-04-02T00-04-02' -split '_')[1]
    $time2 = $time -split 'T' 

    Output: 

    2018-04-02
    00-04-02

    Can you tell me how I can combine the result of next below 2 commands to make the output in single line as 2018-04-02 00:04:02?

    $dateparts = $time2[0] -split '-' 
    $time2[1] -split '-'

    Tuesday, April 3, 2018 6:57 PM
  • That is correct.  That is how it works.


    \_(ツ)_/

    Tuesday, April 3, 2018 7:17 PM
  • You can try this instead:

    $time = ('Office_2018-04-02T00-04-02' -split '_')[1] -split 'T'
    $time2 = $time[1] -replace "-", ":"
    $time3 = $time[0], $time2 -join " "

    If you want a [datetime] object instead of the string, add this line to the bottom:

    $dt=[datetime]::parseexact($time3, 'yyyy-MM-dd h:mm:ss', $null)


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by jrv Wednesday, April 4, 2018 1:53 AM
    Tuesday, April 3, 2018 7:47 PM
  • Skip the parse:

    PS D:\scripts> $time = ('Office_2018-04-02T00-04-02' -split '_')[1] -split 'T'
    PS D:\scripts> $time2 = $time[1] -replace "-", ":"
    PS D:\scripts> $time3 = $time[0], $time2 -join " "
    PS D:\scripts> [datetime]$time3
    
    Monday, April 2, 2018 12:04:02 AM
    
    
    PS D:\scripts>


    \_(ツ)_/

    Tuesday, April 3, 2018 8:03 PM
  • I did below dumb code to get what I wanted

    $FileName = "Office_2018-04-02T00-09-02.csv"

    $time = ($FileName -split '_')[1]

    $time = $time.Replace(".csv","")

    Remove-Item V:\SQLFeed\SplitOutput.txt -force

    $time -split 'T' >> V:\SQLFeed\SplitOutput.txt

    $Join = Get-Content V:\SQLFeed\SplitOutput.txt -ReadCount 3 | ForEach{$_ -Join " "}

    $DatePart = $Join.Substring(0, $Join.IndexOf(" "))

    $TimePart = $Join | % {$_.substring($_.length-8) -replace ("-",":")}

    $DatePart + " " + $TimePart

    All this to convert Office_2018-04-02T00-09-02.csv to 2018-04-02 00:09:02

    Tuesday, April 3, 2018 8:14 PM
  • My Perl sensibilities are offended. Too many variables! No data validation! :-)

    $FileName = "Office_2018-04-02T00-09-02.csv"
    $DateString = $null
    
    Try {
        $DateString = $Filename -replace '^Office_(\d{4}-\d\d-\d\d)T(\d?\d)-(\d\d)-(\d\d)\.csv$', '$1 $2:$3:$4'
        write-host $DateString
        [datetime]$DateString
    } Catch {
        Write-Host "Bad filename ($FileName). Should throw exception/error here!"
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by jrv Wednesday, April 4, 2018 1:53 AM
    Tuesday, April 3, 2018 11:14 PM
  • It takes a village!

    \_(ツ)_/

    Wednesday, April 4, 2018 1:53 AM