Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: New-Timespan format result

Tags:

powershell

When I´m using the folling PowerShell code, I´m getting results like 123.0000232 for example.

Is is there a paramter to format the result?

I want a result like 123

$TimeStart = Get-Date
#some code
sleep 2
(New-TimeSpan -Start $TimeStart -End $(Get-Date)).TotalSeconds

Thank you in advance

like image 301
LaPhi Avatar asked Nov 25 '14 09:11

LaPhi


People also ask

How do I get the current date in powershell?

The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several . NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.


1 Answers

You could do one of two things:

Use the seconds attribute, which is an int32

(New-TimeSpan -Start $TimeStart -End $(Get-Date)).Seconds

or Typecast TotalSeconds to [int32] (not so nice, but works too)

[int32] (New-TimeSpan -Start $TimeStart -End $(Get-Date)).TotalSeconds
like image 89
Micky Balladelli Avatar answered Oct 11 '22 10:10

Micky Balladelli