locked
Changing date format in a log file RRS feed

  • Question

  • I have a property I write to a log file like this

     Out-File -FilePath $LogFile -Append -InputObject ("CREATED: " + $Alert.TimeRaised.AddHours(2))

    I add hours because of the time zone, just ignore that...

    Anyway, what is written to the log file is in this format, and I would like to change it to day/month/year, instead of month/day/year

    CREATED: 03/31/2016 02:54:09

    Can i use any of the methods available for this object?


    Freddy

    Tuesday, April 12, 2016 8:00 AM

Answers

  • mm is used for minutes, while MM is used for month. correct one should be:

    $Alert.TimeRaised.AddHours(2).ToString("dd/MM/yyyy H:mm:ss")


    cheers..uc

    • Marked as answer by CesarTabares Tuesday, April 12, 2016 8:36 AM
    Tuesday, April 12, 2016 8:20 AM

All replies

  • There are different ways to format date/time. for your requirement (day/month/year) it will be like this:

    (get-date).ToString("d/M/yyyy")
    Get-Date -Format 'd/M/yyyy'
    

    Refer to following article for detailed date/time formats available:

    https://technet.microsoft.com/en-us/library/ee692801.aspx


    cheers..uc

    Tuesday, April 12, 2016 8:12 AM
  • Hi Freddy,

    Yes, you can use the .ToString() method as illustrated below:

    $Alert.TimeRaised.AddHours(2).ToString("dd/mm/yyyy H:mm:ss")

    That's assuming you want 24 hour time format.

    Cheers,
    Lain

    Tuesday, April 12, 2016 8:14 AM
  • mm is used for minutes, while MM is used for month. correct one should be:

    $Alert.TimeRaised.AddHours(2).ToString("dd/MM/yyyy H:mm:ss")


    cheers..uc

    • Marked as answer by CesarTabares Tuesday, April 12, 2016 8:36 AM
    Tuesday, April 12, 2016 8:20 AM
  •  Hi Cesar, 

    You can use the below one. 

    Get-Date -UFormat "%d/%m/%Y %H:%M:%S"


    Tuesday, April 12, 2016 8:32 AM
  • I got the answer from UCTHEV, but i was just curious about this one aswell.

    How would you use any of the cmdlets suggested in this thread

    Get-Date -UFormat "%d/%m/%Y %H:%M:%S"
    (get-date).ToString("d/M/yyyy")
    Get-Date -Format 'd/M/yyyy'

    in my code? I don't get it, sorry about that


    Freddy

    Tuesday, April 12, 2016 8:41 AM
  • The same way I listed it the first time - minus the lazy mistake on the case for the month.

    Just change the part of your existing line from:

    $Alert.TimeRaised.AddHours(2)

    to:

    $Alert.TimeRaised.AddHours(2).ToString("dd/MM/yyyy H:mm:ss")

    Cheers,
    Lain

    Tuesday, April 12, 2016 8:57 AM