Alan
Alan

Reputation: 1637

Why does ParseExact in my PowerShell script accept an int64 and not a string?

Although I was able to achieve my objective, I don't understand why and I am concerned I'm still doing something wrong.

The context is to retrieve a datetime value for comparison to another date using FTP to retrieve a file's timestamp. The datetime is received as a string using this:

$FTPrequest = [System.Net.FtpWebRequest]::Create($FTPTargetFile)
$FTPrequest.Method = [System.Net.WebRequestMethods+FTP]::GetDateTimestamp
$FTPrequest.Credentials = $Credentials
$response = $FTPrequest.GetResponse().StatusDescription
$tokens = $response.Split(" ")
if ($tokens[0] -eq 213) {
    $Timestampdata = $tokens[1]
} else {
    Write-Output "FTP timestamp request for " + $FTPTargetFile + " returned an error"
}

Almost every available resource I found made it clear that ParseExact uses a string to derive a datetime as in the following line of script:

$d = [DateTime]::ParseExact($Timestampdata,"yyyyMMddHHmmss",$null)

But whether in a script or at the command prompt, the line above consistently returns the following error:

Exception calling "ParseExact" with "3" argument(s): "String was not recognized
as a valid DateTime."
At C:\hw-sw\powershell\FTPupload option - getFileInfo.ps1:38 char:1
+ $d = [DateTime]::ParseExact($Timestampdata,"yyyyMMddHHmmss",$null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

There were 2 links I found (lin1, link2 - 2nd link addresses a slighty different problem but the point is still made) that suggested ParseExact() uses an intXX instead, as in the following line of script:

$d = [DateTime]::ParseExact([convert]::ToInt64($Timestampdata),"yyyyMMddHHmmss",$null)

And yes, the line above runs without error.

While I am grateful for having found a potential solution to my scripting problem, I am unsettled because I don't understand why ParseExact is working with an int64, and not a string as I would have expected.

Is there some setup in PowerShell that changes how it works? I believe I must be making some simple mistake, but I can't figure it out.

Upvotes: 0

Views: 1139

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

static datetime ParseExact(string s, string format, System.IFormatProvider provider)
static datetime ParseExact(string s, string format, System.IFormatProvider provider, System.Globalization.DateTimeStyles style)
static datetime ParseExact(string s, string[] formats, System.IFormatProvider provider, System.Globalization.DateTimeStyles style)

These are the overloaded methods for ParseExact on PSv5.1. You're passing it a $null provider.

Additional information

Your use case

After more reading, ensure your first argument doesn't have spaces or it will fail. Also make sure your current culture is correct for the behavior you're expecting.

Upvotes: 1

Related Questions