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-25T22:12:07.973+00:00

    $Global:selectProperties=@("$datum.ToLocalTime().ToString("dd-MM-yyyy")","00:00 - 07:59","08:00 - 16:59","17:00 - 23:59","Opmerkingen");
    I get an syntax error

    0 comments No comments

  2. Peter Kiers 66 Reputation points
    2021-03-25T22:23:16.01+00:00

    $Global:seletProperties=@($_["Datum"].ToLocalTime().ToString("dd-MM-yyyy"),"00:00 - 07:59","08:00 - 16:59","17:00 - 23:59","Opmerkingen");
    Doesn't work either

    This is very hard


  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,971 Reputation points Microsoft Vendor
    2021-03-26T08:50:37.823+00:00

    Hi,

    What is the data type of the property "datum"? If it's DateTime you can try this

    ($hashtable.Datum.ToLocalTime()).ToString("dd-MM-yyyy")  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Peter Kiers 66 Reputation points
    2021-03-26T11:39:08.34+00:00

    I have tried the solution from IanXue-MSFT and RichMatheisen-8856 avatar image RichMatheisen-8856
    Even thou both solutions did not work i just want to thank you for look into my problem.

    These are the properties of the columns in my Sharepoint List

    81921-datumtijd.png

    Greetings, Peter