Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the # of days difference between two dates in Powershell

Tags:

powershell

I am trying to get the number of days difference in Windows powershell, I am extracting the last date of the year i.e.. 20171231(yyyyMMdd) from a text file that I have locally stored the date in that file.

Here is the below code that I am trying but not able to get the difference of the days, am getting the wrong output by directly subtracting, if am converting the string extracted from the file and then subtract it with the date type, even then am getting the wrong output.

$DateStr = (Get-Date).ToString("yyyyMMdd")

$content = Get-Content C:\Users\Date.txt 

$diff = $content.ToString();

#Convert it to the date type
$diff = [datetime]::parseexact($diff, 'yyyyMMdd', $null)

#$diff3 = $diff - $DateStr
like image 766
Karan Avatar asked May 24 '17 07:05

Karan


People also ask

Who should not take the Covid vaccine?

According to the CDC, anyone who has a severe allergy (e.g., anaphylaxis) to any of the mRNA vaccine ingredients should not receive this vaccine. The CDC says people with allergies to certain foods, insects, latex and other common allergens can safely receive the COVID-19 vaccine.

Is it OK to take ibuprofen after Covid vaccine?

Apply a clean, cool, wet washcloth to your arm to help reduce pain. It may also help to exercise your arm. If you have a fever, drink plenty of fluids and dress lightly. Over-the-counter medicines like Tylenol® (acetaminophen) or Motrin® or Advil® (ibuprofen) can help with pain, fever, headache, or discomfort.

How long after having Covid can you get it again?

Studies suggest that reinfection with SARS-CoV-2 with the same virus variant as the initial infection or reinfection with a different variant are both possible; early reinfection within 90 days of the initial infection can occur.

What happens if you take Tylenol before Covid vaccine?

You should not take over-the-counter pain relievers such as aspirin, ibuprofen, or acetaminophen to help prevent side effects before you get the vaccine. It is not known if these medicines will affect how well the vaccine works.


3 Answers

Use New-TimeSpan as it represents a time interval. Like so,

$d1 = '2017-01-01'
$d2 = '2017-05-01'
$ts = New-TimeSpan -Start $d1 -End $d2
$ts.Days # Check results
120
like image 169
vonPryz Avatar answered Nov 06 '22 05:11

vonPryz


A quick, dirty, one line powershell script to get the difference between current date and any future date:

[math]::Ceiling((([DateTime]'mm-dd-yyyy')-(Get-Date)).TotalDays)
like image 5
kenguil-MSFT Avatar answered Nov 06 '22 05:11

kenguil-MSFT


$DateStr is going to be a string, so it can't be parsed as a date. You can also use new-timespan to get the difference between two dates.

$Date = Get-Date

$diff = Get-Content C:\Users\Date.txt -raw

#Convert it to the date type
$diff = [datetime]::parseexact($diff, 'yyyyMMdd', $null)

$diff3 = New-TimeSpan -Start $diff -end $Date

#Number of days
$diff3.days
like image 2
Anthony Allen Avatar answered Nov 06 '22 06:11

Anthony Allen