简体   繁体   中英

Converting a string to date-time format

I am importing a csv to an excel using powershell. Both of my columns have date-time in the following format:

2019-01-25T19:58:28.000Z

I want to convert the column to the following format "dd/MM/yyyy h:mm"

I tried the following two things and none of them worked: [datetime]::ParseExact($fullinfo.A, "dd/MM/yyyy h:mm", $null)

$excel.Columns.item('b').NumberFormat = "dd/MM/yyyy h:mm"

$fullInfoSheet = $workbook.Worksheets.Item(1)

$fullInfoSheet.cells.item(1,1) = "Column A" 
$fullInfoSheet.cells.item(1,2) = "Column B" 

$fullinfolist = Import-Csv -Path $csvfullFile -Header A, B

$i = 2 

foreach($fullinfo in $fullinfolist) 
{ 
 $fullInfoSheet.cells.item($i,1) = [datetime]::ParseExact($fullinfo.A, "dd/MM/yyyy h:mm", $null) 
 $fullInfoSheet.cells.item($i,2) = $fullinfo.B 
 $i++ 
}

$excel.Columns.item('b').NumberFormat = "dd/MM/yyyy h:mm"

This produces output in your desired format

$InputDate = '2019-01-25T19:58:28.000Z'

get-date $InputDate -Format 'dd/MM/yyyy h:mm'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM