Search code examples
powershellresthtml-email

Creating an email with Powershell - I need date format corrected


I am using Invoke-RestMethod to get data for an email about POs. It is working.

However, my arrival date column looks ugly:

ARR_Arrived_Date

10/16/2019 12:00:00 AM
10/10/2019 12:00:00 AM
10/9/2019 12:00:00 AM
10/8/2019 12:00:00 AM

I don't want the time, just the short date.

Here is the pertinent code:

$data = Invoke-RestMethod -Uri https://XXXXXXX/api/v1/BaqSvc/ArrivedPO/ -Credential $Cred -ContentType "Application/Json; charset=UTF-8"

$Header = @" <style> TABLE {border-width: 1px; border-style: solid;
border-color: #CCCCCC; border-collapse: collapse;} TH {border-width:
1px; padding: 3px; border-style: solid; border-color: black;
background-color: #00FF00;} TD {border-width: 1px; padding: 3px;
border-style: solid; border-color: black;} </style> "@

$message.IsBodyHTML = $true 

$message.Body = $data.value |
    ConvertTo-Html -Property ARR_PONum,ARR_PackSlip,ARR_Vendor_Name,ARR_Arrived_Date,ARR_Days_Arrived,ARR_FirstName,ARR_LastName,ARR_OrderNum,ARR_OpportunityID,ARR_Name -Head $Header

I don't know how to address ARR_Arrived_Date column for date formatting. Please help!


Solution

  • You can use Calculated Properties, with DateTime.ParseExact, See an example:

    $message.Body = $data.value |
    ConvertTo-Html -Property ARR_PONum,ARR_PackSlip,ARR_Vendor_Name,
    @{N="ARR_Arrived_Date";E={[DateTime]::ParseExact($_.ARR_Arrived_Date ,"MM/dd/yyyy hh:mm:ss tt",$null)}},
    ARR_Days_Arrived,ARR_FirstName,ARR_LastName,ARR_OrderNum,
    ARR_OpportunityID,ARR_Name -Head $Header