Changing the date format in powershell

Hi, I am importing multiple excel files using Import-Excel. Unfortunately, the date- time format gets put in to 10 decimal places when the the software the result gets fed into only works to 5 decimal places. Is there anyway to change the date to only 5 decimal places

Hi, welcome to the forum :wave:

What format is the date column in the Excel document? Dates should just be DateTime objects.

PS E:\Temp> (Import-Excel .\TestDate.xlsx).Date

13 August 2024 00:00:00

PS E:\Temp> (Import-Excel .\TestDate.xlsx).Date | Get-Member

   TypeName: System.DateTime

Hi Matt, in the excel file the date is in the custom format dd/MM/yyyy HH:mm:ss.

You will lose some accuracy which may affect the seconds, but you can use the Round() method from the [Math] class.

$Date = (Import-Excel .\TestDate.xlsx).Date
PS E:\Temp> $Date
45517.6829282407

E:\Temp> $Date = [Math]::Round($Date, 5)
PS E:\Temp> $Date
45517.68293

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.