-match a system.date with a string RRS feed

  • Question

  • Hi,

    I've got a system.date file variable that I comes back as an object and I want to match it to a variable in date format that is a string.  Is this possible?

    Here is my code:

    $dtime = [datetime]::parseExact($USDate, "dd/MM/yyyy",$null)
    $USDate = $dtime | get-date -format dd/MM/yyyy

    If I write-host on $USDate it comes back as follows (as a string)


    The system.date object looks like this and is a true system date:

    LastModified:   30/09/2018 02:26:24

    When I try and match the 2 it never finds it  I've tried -like and -match which technically should work.

    Here is my where clause:

    $count = get-s3object -bucketname $bucketname | where {$_.key -like 'ierter/*' -and $_.StorageClass -like 'Standard' -and $_.lastmodified -like $USDate}

    Monday, October 1, 2018 4:42 PM

All replies

  • For dates you must use two date objects and you can not use "match"

    $obj.LastModified -lt [datetime]::Today

    This will test the dates.

    You can also do this:

    $d = [datetime]'10/01/2018'
    [datetime]::Today -gt $d


    Monday, October 1, 2018 4:57 PM
  • This works for me.  -like needs a "*".

    $usdate = '01/01/2018'
    $lastmodified = [datetime]'1/1/2018 12:00'

    01/01/2018 12:00:00

    $lastmodified -match $usdate

    $lastmodified -like "$usdate*"

    • Edited by JS2010 Monday, October 1, 2018 10:32 PM
    Monday, October 1, 2018 6:57 PM