Date time format confusion ?

Hi,

Using just single date time variable like below which is the part of parameter format to search through some log files below:

[string] $StartDate = (Get-Date (Get-Date).AddDays(-30) -F "MM/dd/yyyy")
[string] $EndDate = (Get-Date -F "MM/dd/yyyy")

I need some assistance in formatting date time stamp based on dd/mm/YYYY as the display format in the console:

Write-host "Time range between $([datetime] $StartDate) until $([datetime]$EndDate):"

the above result is just showing:

Time range between 05/02/2018 00:00:00 until 06/01/2018 00:00:00:

So how can I get the result as DD/MM/YYYY ?

  • The results you are getting, is because that is what you defined.

    If you want a differ format, then you have to ask for that. Also, there is no real reason to strongly type this data just to get what you are after.

    Here is the guidance you need on date formatting topic.

    Formatting Date Strings with PowerShell
    'blogs.technet.microsoft.com/heyscriptingguy/2015/01/22/formatting-date-strings-with-powershell'

    PowerTip: Use PowerShell to Format Dates
    'blogs.technet.microsoft.com/heyscriptingguy/2013/11/11/powertip-use-powershell-to-format-dates'

    PowerShell Get-Date formatting
    'jasonpearce.com/2016/06/08/powershell-get-date-formatting'

    Also, remember that Write-Host empties the buffer, so, if you find later you are using the pipeline, it won't be there.

    If you are just writing to the screen, you can just use the text. Write-Host is not needed to just write to the screen, It's needed for colorizing text and or other specific string screen formatting needs. Dates are not really one of them in my experience to date.