Home Forums Programming Powershell datetime and string conversion problem RE: datetime and string conversion problem

  • Hi Shawn

    Thanks so much for your help once again.

    We actually managed to get this working using the same method you first tried - wrote this function:

    Function FDate {

    param(

    [Parameter(Mandatory = $false)] $param

    )

    #check for null values in the date field. Different to normal null verification

    if ($param -eq [System.DBNull]::Value){

    $correctdateformat = ""}

    else {

    $correctdateformat = $param | get-date -format "yyyy-MM-dd hh:mm:ss:fff"}

    return $correctdateformat

    }

    This works fine in our scenario - although initially i had issues when expecting a [DateTime] object for the parameter of the function. After removing this it worked. I never saw the error you are getting for some reason.

    The only issue is you are then required to call this function for each field you need formatted in this way:

    foreach ($d in $data){

    $SessionIdTime = FDate($d.SessionIdTime)

    qry = "insert into $table VALUES ('$SessionIdTime', '$($d.SessionIdSeq)', '$InviteTime', '$FromUri', '$ToUri'.......

    etc }

    Your method looks much more efficient.

    Thanks again for your help