none
Get-Date auto corrects days up until 31 RRS feed

  • Question

  • Hi All,

    Just wondering if anybody already noticed this behavior of the Get-Date command-let

    And if it's possible to force get-date to give an error on incorrect dates.

    The script below is an example:

    # non existing/invalid date time
    $InputDate = "31-06-2017"
    $arrDateTime = ($InputDate).Split("-")
    $Date = Get-Date -Day $arrDateTime[0] -Month $arrDateTime[1] -Year $arrDateTime[2]
    
    If($Date.Day -ne $arrDateTime[0])
    {
        "Incorect date object $($Date)"
        "Do you want to use $($Date.AddDays(-$Date.Day))?"
    }Else{
       "Correct Date object $($Date)"
    }

    June 31 does not exist I would expect an error. But instead the get-date just changes the date to the 1 July.
    February 31 is corrected to March 3
    February 32 give's an error as expected

    For now not really issue but something to keep in mind when working with the Get-Date command-let

    Friday, May 12, 2017 8:50 AM

Answers

  • This is a very flexible method for testing and converting string dates of any format.

    Try{
    	#good date
    	$d = '30-06-2017'
    	$dt = [datetime]::ParseExact($d, 'dd-MM-yyyy', [cultureinfo]::InvariantCulture)
    	Write-Host "Date $d" -ForegroundColor green
    	
    	$d = '31-06-2017'
    	#bad date
    	$dt = [datetime]::ParseExact($d, 'dd-MM-yyyy', [cultureinfo]::InvariantCulture)
    	Write-Host "Date $d" -ForegroundColor green
    }
    Catch{
    	Write-Host "Bad date $d" -fore red
    }
    If the date is not valid for the culture then it will throw an exception.


    \_(ツ)_/


    • Edited by jrv Friday, May 12, 2017 9:18 AM
    • Marked as answer by Rene5380 Friday, May 12, 2017 9:28 AM
    Friday, May 12, 2017 9:16 AM

All replies

  • Hi Rene,

    nope, it will automatically roll over. But you can go a different route:

    New-Object System.DateTime($arrDateTime[2], $arrDateTime[1], $arrDateTime[0])

    Cheers,
    Fred

    PS: Don't split hairs or strings unless you have to:

    [datetime]::ParseExact("31-06-2017", "dd-MM-yyyy")
    [datetime]::ParseExact("30-06-2017", "dd-MM-yyyy")


    There's no place like 127.0.0.1


    • Edited by FWN Friday, May 12, 2017 9:06 AM
    • Proposed as answer by Paweł Balczunas Friday, May 12, 2017 9:08 AM
    Friday, May 12, 2017 9:03 AM
  • This is a very flexible method for testing and converting string dates of any format.

    Try{
    	#good date
    	$d = '30-06-2017'
    	$dt = [datetime]::ParseExact($d, 'dd-MM-yyyy', [cultureinfo]::InvariantCulture)
    	Write-Host "Date $d" -ForegroundColor green
    	
    	$d = '31-06-2017'
    	#bad date
    	$dt = [datetime]::ParseExact($d, 'dd-MM-yyyy', [cultureinfo]::InvariantCulture)
    	Write-Host "Date $d" -ForegroundColor green
    }
    Catch{
    	Write-Host "Bad date $d" -fore red
    }
    If the date is not valid for the culture then it will throw an exception.


    \_(ツ)_/


    • Edited by jrv Friday, May 12, 2017 9:18 AM
    • Marked as answer by Rene5380 Friday, May 12, 2017 9:28 AM
    Friday, May 12, 2017 9:16 AM
  • I we know the culture origin of the date strings then this also works:

    # good date
    PS>[datetime]::Parse('30-06-2017',[cultureinfo]::CreateSpecificCulture('fr-FR'))
    
    Friday, June 30, 2017 12:00:00 AM
    
    # bad date
    PS>[datetime]::Parse('31-06-2017',[cultureinfo]::CreateSpecificCulture('fr-FR'))
    Exception calling "Parse" with "2" argument(s): "String was not recognized as a valid DateTime."

    This allows for all date formats allowed in that culture.

    PS>[datetime]::Parse('30.06.2017',[cultureinfo]::CreateSpecificCulture('fr-FR'))
    
    Friday, June 30, 2017 12:00:00 AM
    
    
    PS>[datetime]::Parse('2017.06.30',[cultureinfo]::CreateSpecificCulture('fr-FR'))
    
    Friday, June 30, 2017 12:00:00 AM


    \_(ツ)_/


    • Edited by jrv Friday, May 12, 2017 9:24 AM
    Friday, May 12, 2017 9:21 AM
  • Thank you FWN,

    Looks like I need to supply the cultural info when using parse exact

    Below is working for me:

    [DateTime]::ParseExact("31-06-2017","dd-MM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture)
    [DateTime]::ParseExact("30-06-2017","dd-MM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture)

    Friday, May 12, 2017 9:27 AM
  • Thank you FWN,

    Looks like I need to supply the cultural info when using parse exact

    Below is working for me:

    [DateTime]::ParseExact("31-06-2017","dd-MM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture)
    [DateTime]::ParseExact("30-06-2017","dd-MM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture)

    If you know the culture of origin use my second example:

    PS>[datetime]::Parse('2017.06.30',[cultureinfo]::CreateSpecificCulture('fr-FR'))
    
    Friday, June 30, 2017 12:00:00 AM
    
    
    PS>[datetime]::Parse('30-6-2017',[cultureinfo]::CreateSpecificCulture('de-GR'))
    
    Friday, June 30, 2017 12:00:00 AM
    

    This gives more possible correct formats automatically  and does not require a mask to force the format.



    \_(ツ)_/

    Friday, May 12, 2017 9:36 AM