Date Comparison - Strangely failing

Welcome Forums General PowerShell Q&A Date Comparison - Strangely failing

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 week, 6 days ago.

  • Author
    Posts
  • #127016

    Participant
    Points: 38
    Rank: Member

    Look at the below code. One is working with $var.ToString() comparing, other without it is not working. Is there any unsaid logic behind the scenes.? 11/28/2018 is date today. I tried using [datetime] in front of variables and it didnt make any difference.

    #Invalid output - False
    $string = "11/28/2018"
    $string1 = Get-Date $string -Hour 00 -Minute 00 -Second 00
    $string2 = Get-Date -Hour 00 -Minute 00 -Second 00
    $string1 -ge $string2
    #works perfect - True
    $string = "11/28/2018"
    $string1 = Get-Date $string -Hour 00 -Minute 00 -Second 00
    $string2 = Get-Date -Hour 00 -Minute 00 -Second 00
    $string1.ToString() -ge $string2.ToString()
  • #127419

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    In the first situation, I'm guessing the objects aren't exactly alike. Dates generated that way can get a little weird, honestly. A proper DateTime object has sub-second resolution, which you're not accounting for (and which normally wouldn't display).

    In the second, you're not comparing dates. You're comparing literal strings. ToString() is what's used to display a DateTime object on-screen, and it ignores sub-second resolution, so you're kind of "equalizing" the two objects by doing that.

  • #127427

    Participant
    Points: 38
    Rank: Member

    Yes 🙂 I figured out that milliseconds is hidden... I fixed it by using -format d..

     

    I couldn't update the comment since the post was waiting for moderator. I am excited talking to you Don Jones. Your book learn powershell in a month is an inspiration. It made me write code from one line commands to thousands lines of scripts ...

     

    But I have a question.. Why Microsoft is not building Excel modules so far while Doug Finke has been doing it with ImportExcel and I understand Epplus.dll is open source. I am sure Ms can use their own interoperability or excel core. Just wondering if you have any answers already 🙂

    • #127436

      Keymaster
      Points: 1,704
      Helping HandTeam Member
      Rank: Community Hero

      The last time MS invested in Office Automation was in 2010, I think. It's just not where they want to spend their time and money. Even then, they just put a thin .NET wrapper around the 2003-era COM stuff. Fortunately, guys like Doug do a great job.

    • #127445

      Participant
      Points: 38
      Rank: Member

      Thanks. However , not all organizations allow to install modules from untrusted repository . That's why I was hoping Ms would come up with their own . Sadly, seems we have to wait . Anyways, thank you very much for the responses. I have been watching your pluralsight videos too . Just read your about me webpage. You are a great personality.

      Keep it up.. Will catch up if powershell wants me to put another QnA 🙂

  • #127430

    Participant
    Points: 290
    Helping Hand
    Rank: Contributor

    As Don says, there's more data there you're going to have a bit of difficulty excluding in this manner. DateTime lets you retrieve just the Date you want to check for, though, which can be neatly compared in the same way:

    $string = "11/28/2018"
    $Date1 = (Get-Date $string).Date
    $Date2 = (Get-Date).Date
    $Date1 -ge $Date2
  • #127434

    Participant
    Points: 38
    Rank: Member

    Thanks Joel . I got it working.

You must be logged in to reply to this topic.