How to convert the date to this format: [“Datum”].ToLocalTime().ToString(“dd-MM-yyyy”)?

Peter Kiers 66 Reputation points
2021-03-25T19:15:42.46+00:00

I need to convert the date used in the function below to this format:

["Datum"].ToLocalTime().ToString("dd-MM-yyyy");

This is the function:

$Global:selectProperties=@("Datum","00:00 - 07:59","08:00 - 16:59","17:00 - 23:59","Opmerkingen");
function ExportList($listName)
{
try
{
$listItems=(Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues
$outputFilePath="c:\Temp\" + $listName + ".xlsx"
$hashTable=@()
foreach($listItem in $listItems)
{
$obj=New-Object PSObject
$listItem.GetEnumerator() | Where-Object { $.Key -in $Global:selectProperties }| ForEach-Object{ $obj | Add-Member Noteproperty $.Key $_.Value}
$hashTable+=$obj;
$obj=$null;
}

    $hashtable | Export-XLSX $outputFilePath -Table -Autofit -Force
 } 
 catch [Exception] 
 { 
    $ErrorMessage = $_.Exception.Message        
    Write-Host "Error: $ErrorMessage" -ForegroundColor Red         
 } 

}

Greetings, Peter

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,091 Reputation points
    2021-03-25T19:44:45.62+00:00

    Is this what you're trying to do?

    $Global:selectProperties = @("Datum", "00:00 - 07:59", "08:00 - 16:59", "17:00 - 23:59", "Opmerkingen");
    function ExportList($listName) {
        try {
            $listItems = (Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues
            $outputFilePath = "c:\Temp\" + $listName + ".xlsx"
            $hashTable = @()
            foreach ($listItem in $listItems) {
                $obj = New-Object PSObject
                $listItem.GetEnumerator() | 
                    ForEach-Object{
                        if ($Global:selectProperties -contains $_.Key){
                            if ($_.Key -eq 'Datum'){
                                $v = "{0:dd-MM-yyyy}" -f ([datetime]($_.Value)).ToLocalTime()
                            }
                            else{
                                $v = $_.Value
                            }
                            $obj | Add-Member Noteproperty $_.Key $v
                    }
                $hashTable += $obj;
                $obj = $null;
            }
            $hashtable | Export-XLSX $outputFilePath -Table -Autofit -Force
        } 
        catch [Exception] { 
            $ErrorMessage = $_.Exception.Message        
            Write-Host "Error: $ErrorMessage" -ForegroundColor Red         
        } 
    }
    
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Peter Kiers 66 Reputation points
    2021-03-26T21:08:01.917+00:00

    Found the answer:

    $Global:selectProperties=@("Datum","00:00 - 07:59","08:00 - 16:59","17:00 - 23:59","Opmerkingen");
    function ExportList($listName)
    {
    try
    {
    $listItems=(Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues
    $outputFilePath="c:\Temp\" + $listName + ".xlsx"
    $hashTable=@()
    foreach($listItem in $listItems)
    {
    $obj=New-Object PSObject
    $listItem.GetEnumerator() | Where-Object { $.Key -in $Global:selectProperties } |
    ForEach-Object {
    if( $
    .Key -eq 'Datum' )
    {
    $obj | Add-Member Noteproperty $.Key $.Value.ToLocalTime().ToString("dd-MM-yyyy")
    }
    else
    {
    $obj | Add-Member Noteproperty $.Key $.Value
    }
    }
    $hashTable+=$obj;
    $obj=$null;
    }

        $hashtable | Export-XLSX $outputFilePath -Table -Autofit -Force
     } 
     catch [Exception] 
     { 
        $ErrorMessage = $_.Exception.Message        
        Write-Host "Error: $ErrorMessage" -ForegroundColor Red         
     } 
    

    }

    Greetings,

    Peter

    0 comments No comments