# Date time comparison issue

**URL:** <https://forums.powershell.org/t/date-time-comparison-issue/21328>\
**Category:** PowerShell Help\
**Created:** [January 10, 2023, 6:49am UTC](https://forums.powershell.org/t/date-time-comparison-issue/21328 "2023-01-10T06:49:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [January 10, 2023, 6:49am UTC](https://forums.powershell.org/t/date-time-comparison-issue/21328/1 "2023-01-10T06:49:32Z")

</div>

Hi  
In order to disable leavers I need to check if the accountexpirationdate is less then or equal to today  
I run the following if statement in a foreach block

```auto
$mainCsvPath = "C:\Users\Public\Documents\CSV\offboarding"
$withemployeeID = import-csv $maincsvpath\problem.csv
$date = (get-date)
$today = [DateTime]::ParseExact($date,'MM/dd/yyyy',$null)
foreach($Luser in $withemployeeID ){
     $AccountExpired = [DateTime]::ParseExact($Luser.AccountExpirationDate,'MM/dd/yyyy',$null)

   
    $Employeenumber = $Luser.EmployeeNumber
      if(!($employeenumber -eq "")){
          if($accountexpired -eq ""){
              [PSCustomObject]@{
                  EmployeeNumber = $Luser.EmployeeNumber    
                  SamAccountName = $Luser.SamAccountName
                  EmailAddress = $Luser.EmailAddress
                  UserPrincipalName = $Luser.UserPrincipalName
                  AccountExpirationdate = $accountexpired
                  Manager = $Luser.Manager
                  Treatmentdate = $today
                  } | Export-csv -path $mainCsvPath\incorrectdata.csv -notype -Append  
          }Else{
           
              if($accountexpired -lt $today){
                  $AccountExpired = $Luser.AccountExpirationdate
                  $accountexpired = (get-date $accountexpired).AddDays(-1)
                  $accountexpired = (get-date $accountexpired).tostring("yyyy-MM-dd")
                  [PSCustomObject]@{
                  EmployeeNumber = $Luser.EmployeeNumber    
                  SamAccountName = $Luser.SamAccountName
                  EmailAddress = $Luser.EmailAddress
                  UserPrincipalName = $Luser.UserPrincipalName
                  AccountExpirationdate = $accountexpired
                  Manager = $Luser.Manager
                  Treatmentdate = $today
                  } | Export-csv -path $mainCsvPath\leaversFinal.csv -notype -Append
              }Else{

                if($accountexpired -eq $today){
                  $AccountExpired = $Luser.AccountExpirationdate
                  $accountexpired = (get-date $accountexpired).AddDays(-1)
                  $accountexpired = (get-date $accountexpired).tostring("yyyy-MM-dd")
                  [PSCustomObject]@{
                  EmployeeNumber = $Luser.EmployeeNumber    
                  SamAccountName = $Luser.SamAccountName
                  EmailAddress = $Luser.EmailAddress
                  UserPrincipalName = $Luser.UserPrincipalName
                  AccountExpirationdate = $accountexpired
                  Manager = $Luser.Manager
                  Treatmentdate = $today
                  } | Export-csv -path $mainCsvPath\leaversFinal.csv -notype -Append
              }
          }
        }
    }

  }

my csv looks like this 

```

“EmployeeNumber”,“SamAccountName”,“EmailAddress”,“UserPrincipalName”,“AccountExpirationDate”,“Manager”,“Treatmentdate”  
“[600027",“samaccountname”,"user@mydomain.com](mailto:600027%22,%22samaccountname%22,%22user@mydomain.com)”,"user@mydomain.com",“10/30/2022 12:00:00 AM”,“myManager”,"

```auto

```

but when I run the code it does not validate the if statement ($accountexpired -lt $today) as being True what am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 10, 2023, 8:18am UTC](https://forums.powershell.org/t/date-time-comparison-issue/21328/2 "2023-01-10T08:18:58Z")

</div>

> [@acer4605](#):
>
> but when I run the code it does not validate the if statement ($accountexpired -lt $today) as being True what am I doing wrong here?

Don’t you get error messages running this code? 🤔 Even when I switch my language/culture setting to `en-GB` or `en-US` I get errors.

Anyway … the way you use `[DateTime]` types looks weird to me. If you want to compare these types by date without worrying about the time part you set the time part to `0` … like this

```auto
(Get-Date).Date

```

If you do the same to the input string from your CSV file you should be able to get correct comparison results:

```auto
(Get-Date '10/30/2022 12:00:00 AM').Date

```

If you want to manipulate these dates and output them in a certain string format you can do that with one line. You don’t need multiple variable assignments

```auto
(Get-Date '10/30/2022 12:00:00 AM').Date.AddDays(-1).ToString('yyyy-MM-dd')

```

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [January 12, 2023, 4:57am UTC](https://forums.powershell.org/t/date-time-comparison-issue/21328/3 "2023-01-12T04:57:55Z")

</div>

Hi olaf,  
Yes I do get errors and I try to eliminate them 😄  
thanks for the tip on the get date.

another question as you might see in my code how can I optimize my excisting code in a correct way are there websites tutorials available to do this?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 12, 2023, 5:45am UTC](https://forums.powershell.org/t/date-time-comparison-issue/21328/4 "2023-01-12T05:45:38Z")

</div>

> [@acer4605](#):
>
> Yes I do get errors

So you should share them completely as well (formatted as code as well) along with the code you used. Error messages are an important language feature telling you what’s wrong and sometimes even how to do it better.

> [@acer4605](#):
>
> another question as you might see in my code how can I optimize my excisting code in a correct way

It depends pretty much on what _optimization_ you want. Of course you should debug errors if you have some. Apart from that - if it does the job … 🤷🏽‍♂️

If you’re unsatisfied with your own code you may identify the parts bothering you and work on that.

> [@acer4605](#):
>
> are there websites tutorials available to do this?

Again - it depends on what you want to improve … maybe this:

> **[GitHub - PoshCode/PowerShellPracticeAndStyle: The Unofficial PowerShell Best...](https://github.com/PoshCode/PowerShellPracticeAndStyle)**
>
> The Unofficial PowerShell Best Practices and Style Guide - GitHub - PoshCode/PowerShellPracticeAndStyle: The Unofficial PowerShell Best Practices and Style Guide

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/date-time-comparison-issue/21328/5 "2024-05-16T20:16:04Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


