Why it is like we get different DateTime formats (especially for date separators slash vs hyphen) without using any custom formats when printed on the console and written to a file.
Even when using the same Dotnet's System.DateTime.Now
, I am seeing the difference in the output of C# vs PowerShell.
Following are my observations for C# and Powershell:
C# - On Console
06-08-2018 02:01:11 AM
C# - written to a file
06-08-2018 02:02:32 PM
PowerShell - [System.DateTime]::Now - On Console
06 August 2018 01:54:45 AM
PowerShell - [System.DateTime]::Now.ToLocalTime() - On Console
06 August 2018 01:54:53 AM
PowerShell - Get-Date - On Console
06 August 2018 01:54:57 AM
PowerShell - [System.DateTime]::Now - written to a file
08/06/2018 01:46:35
I am mostly curious about when written to a file case of both C# and PowerShell as observed in the same system.
One more point to be noted - I have also seen the C#'s hyphen getting changed into slash on a different system when running the same exe. I am not sure why this happens and due to this it caused some exceptions. I had to use explicit DateTime format to overcome this problem.
Are there any best practices when dealing with DateTime formats?
======================== UPDATE ====================================
As per suggested by @mjwills:
I found that when explicitly using the Tostring() in PowerShell, the behavior becomes consistent in C# and Powershell even if written to a file through PowerShell.
[System.DateTime]::Now.ToString()