Powershell days different output result is not a day number

%3CLINGO-SUB%20id%3D%22lingo-sub-3348441%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20days%20different%20output%20result%20is%20not%20a%20day%20number%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3348441%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1385466%22%20target%3D%22_blank%22%3E%40gugugu%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20that's%20correct.%20Any%20%5Bdatetime%5D%20arithmetic%20results%20in%20what%20you're%20seeing%2C%20which%20is%20a%20%5Btimespan%5D%20object%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.timespan%3Fview%3Dnet-6.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ETimeSpan%20Struct%20(System)%20%7C%20Microsoft%20Docs%3C%2FA%3E).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20simplest%20way%20to%20get%20the%20difference%20in%20days%20is%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E(%24Date1%20-%20%24Date2).Days%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20value%20of%20Days%20is%20negative%2C%20that%20just%20means%20that%20%24Date2%20is%20greater%20than%20%24Date1.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%2C%3C%2FP%3E%3CP%3ELain%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3348523%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20days%20different%20output%20result%20is%20not%20a%20day%20number%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3348523%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314983%22%20target%3D%22_blank%22%3E%40LainRobertson%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20for%20you%20reply%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20see%20the%20image%20i%20used%20%24date1%20is%202022%2F08%2F05%20%24date2%20is%202022%2F05%2F10.%3C%2FP%3E%3CP%3ESo%20result%20shoud%20not%20a%26nbsp%3B%3CSPAN%3Enegative%20number.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20I%20think%20result%20is%20not%20a%20timestamp.because%20I%20use%20this%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5BDateTime%5D%3A%3AFromFileTimeUTC(%24Date1%20-%20%24Date2).days%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Ecant't%20be%20result%20to%20a%20date.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20got%20the%20same%20result%20using%20New-TimeSpan%2C%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E(New-TimeSpan%20-Start%20%24Date2%20-End%20%24Date1).Days%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Ebut%20what%20MS%20says%20about%20New-TimeSpan%26nbsp%3B%20-days%20shoud%20be%20a%20number%20(Int32).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20get%20the%20result%20be%20a%20%22days%22(number)%20not%20a%20timestamp%20.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehave%20you%20get%20a%20good%20idea%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3348282%22%20slang%3D%22en-US%22%3EPowershell%20days%20different%20output%20result%20is%20not%20a%20day%20number%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3348282%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20I%20use%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Date1%20-%20%24Date2%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eoutput%20result%20is%20not%20a%20time%20number%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Date1%0A%24Date2%0A%24Date1%20-%20%24Date2%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20result%20is%20shown%20as%20below%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22xuedong2180_0-1652152396095.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F370381i1F67A23B91B3E7F4%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22xuedong2180_0-1652152396095.png%22%20alt%3D%22xuedong2180_0-1652152396095.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3Ehow%20can%20i%20get%20days%20in%20a%20number%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3348282%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3348550%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20days%20different%20output%20result%20is%20not%20a%20day%20number%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3348550%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1385466%22%20target%3D%22_blank%22%3E%40gugugu%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%2C%20your%20first%20command%20is%20not%20going%20to%20work%20as%20it's%20mixing%20data%20types.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20call%20to%20FromFileTimeUTC%20is%20expecting%20an%20Int64%2C%20not%20a%20Timespan%20-%20which%20is%20what%20you%20get%20from%20%22%24Date1%20-%20%24Date2%22.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20simplest%20way%20to%20get%20the%20days%20is%20as%20shown%20below.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22LainRobertson_0-1652158601870.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F370390i0ECCBC16D672D0F9%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22LainRobertson_0-1652158601870.png%22%20alt%3D%22LainRobertson_0-1652158601870.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESimilarly%2C%20New-TimeSpan%20returns%20the%20same%20result.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22LainRobertson_1-1652158721258.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F370391i2E5144EE3191502F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22LainRobertson_1-1652158721258.png%22%20alt%3D%22LainRobertson_1-1652158721258.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%2C%3C%2FP%3E%3CP%3ELain%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

When I use

$Date1 - $Date2

output result is not a time number

 

$Date1
$Date2
$Date1 - $Date2

 

the result is shown as below

xuedong2180_0-1652152396095.png

how can i get days in a number?

 

3 Replies

@gugugu 

 

Yes, that's correct. Any [datetime] arithmetic results in what you're seeing, which is a [timespan] object (TimeSpan Struct (System) | Microsoft Docs).

 

The simplest way to get the difference in days is:

 

($Date1 - $Date2).Days

 

If the value of Days is negative, that just means that $Date2 is greater than $Date1.

 

Cheers,

Lain

@LainRobertson 

 

Thank for you reply

 

You can see the image i used $date1 is 2022/08/05 $date2 is 2022/05/10.

So result shoud not a negative number.

 

And I think result is not a timestamp.because I use this

[DateTime]::FromFileTimeUTC($Date1 - $Date2).days

cant't be result to a date.

 

I got the same result using New-TimeSpan,

(New-TimeSpan -Start $Date2 -End $Date1).Days

but what MS says about New-TimeSpan  -days shoud be a number (Int32).

 

I want get the result be a "days"(number) not a timestamp .

 

have you get a good idea?

 

 

 

@gugugu 

 

So, your first command is not going to work as it's mixing data types.

 

The call to FromFileTimeUTC is expecting an Int64, not a Timespan - which is what you get from "$Date1 - $Date2".

 

The simplest way to get the days is as shown below.

 

LainRobertson_0-1652158601870.png

 

Similarly, New-TimeSpan returns the same result.

 

LainRobertson_1-1652158721258.png

 

Cheers,

Lain