'The months difference between dates using PowerShell

I need to have AD User Account Expiration Date and now how many months and date its remain until will be disabled.

I tried the code under but I am getting in the months 1 and I have less than one month I would like to have answer like 0 month and 27 days

$StartDate (DateNow)
2019-08-29 00:00:00

AccountExpirationDate                                                                                                                                                                         
---------------------                                                                                                                                                                         
2019-09-26 00:00:00    

$ExpirDate = Get-ADUser test111 -Properties AccountExpirationDate | select AccountExpirationDate

AccountExpirationDate                                                                                                                                                                         
---------------------                                                                                                                                                                         
2019-09-26 00:00:00    

$EndDate= $ExpirDate.AccountExpirationDate

2019-09-26 00:00:00 


$StartDate = (GET-DATE)

2019-08-29 00:00:00


NEW-TIMESPAN –Start $StartDate –End $EndDate

Days              : 27
Hours             : 10
Minutes           : 29
Seconds           : 56


$monthdiff = $EndDate.month - $StartDate.month + (($EndDate.Year - $StartDate.year) * 12)

1 

(Here I got the number 1 but I have less than one month)



Solution 1:[1]

I found no easy way to do this in PowerShell (as TimeSpan doesn't support month counting), hence I ended up with the following. Starting with the years of the two dates, take their difference and course correct if the start day hasn't passed in the current year. Then do the same with the months:

$StartDate = [DateTime]'2021-01-23'
$today = Get-Date
$daydiff = New-TimeSpan -Start $StartDate -End $today
    
$yeardiff = $today.year - $StartDate.year
If($yeardiff -gt 0 -And $StartDate.month -gt $today.month 
  -And $StartDate.day -gt $today.day) { 
    $yeardiff = $yeardiff -1 
}
    
$monthdiff = $today.month - $StartDate.month + ($yeardiff * 12)
If($StartDate.day -gt $today.day) { $monthdiff = $monthdiff -1 }
    
Write-Host "$($daydiff.days) days | $($monthdiff) months"

Simplest solution I could work out.

Solution 2:[2]

I get the number of months between the start date and the monthly anniversary day in the current month. Then adjust:

$start_date = Get-Date '2022-02-27 21:00'
$end_date = Get-Date

# get the monthly anniversary of the $start_date in the current month
$this_month_anniversary = Get-Date ('{0}-{1}-{2} {3:d2}:{4:d2}' -f $end_date.Year, $end_date.Month, $start_date.Day, $start_date.Hour, $start_date.Minute)

# get the number days in the month, so we can get a denominator when figuring the percent of the way we are towards the next anniversay.
# Which month? If we're past this month's anniversary use the current month. If we haven't reached it yet, use the previous month
if  ($end_date -gt $this_month_anniversary) {
    $days_in_month =  [DateTime]::DaysInMonth($end_date.Year, $end_date.Month)
} else {
    $last_month = (Get-Date ('{0}-{1}-01' -f $end_date.Year, $end_date.Month)).AddDays(-1)
    $days_in_month =  [DateTime]::DaysInMonth($last_month.Year, $last_month.Month)
}

# get months between the start date and this month's anniversay, then
# adjust for the current month, this will be negative if the anniversay hasn't occured yet, otherwise positive
($this_month_anniversary.Month - $start_date.month + (($this_month_anniversary.Year - $start_date.year) * 12) +
    (New-TimeSpan -Start $this_month_anniversary -End $end_date).TotalDays / $days_in_month)

Solution 3:[3]

I believe this is what you want, or can be tweaked to achieve it relatively easily.

$today = Get-Date;
$endOfYearDate = "12/31/$($today.Year)";
$endOfYear = Get-Date($endOfYearDate);
$monthsLeftInTheYear = ($endOfYear.Month - $today.Month);
$daysLeftInTheYear = ($endOfYear - $today);
$daysLefInTheYear.Days;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mikey
Solution 2
Solution 3 Bbb