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,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 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-25T19:56:37.38+00:00

    Yes, its a function that export a Sharepoint list to Excel. The only thing that i would like, is to pass the date format to the property Datum. Which is Dutch for Date. Gr. P


  2. Andreas Baumgarten 96,361 Reputation points MVP
    2021-03-25T20:04:26.81+00:00

    Hi @Peter Kiers ,

    which "Datum"/date are you talking about? The current date? Or where do you get the Datum/date information from?

    $Datum = Get-Date -DisplayHint Date -Format "dd-MM-yyyy"  
    $Datum  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Peter Kiers 66 Reputation points
    2021-03-25T20:11:34.197+00:00

    In the Sharepoint List is a Date column called "Datum" i would like to retrieve the data from that column. And it works. Only it displays it in the wrong format.
    Gr. P


  4. Peter Kiers 66 Reputation points
    2021-03-25T20:14:23.187+00:00

    It should be something like this, only the syntax is wrong:
    $Global:selectProperties=@([datum].ToLocalTime().ToString("dd-MM-yyyy"),"00:00 - 07:59","08:00 - 16:59","17:00 - 23:59","Opmerkingen");