In Windows, you can format the date using the date command in the Command Prompt or PowerShell. However, the formatting options are more limited compared to Linux. Here’s how you can format the date in both environments:
Using Command Prompt
-
Display Current Date:
date /T -
Custom Format:
Windows Command Prompt does not support custom formatting directly like Linux. However, you can use the following command to display the date in a specific format:for /f "tokens=1-3 delims=/ " %a in ('date /T') do @echo %c-%a-%bThis example assumes the date format is MM/DD/YYYY and rearranges it to YYYY-MM-DD.
Using PowerShell
PowerShell provides more flexibility for formatting dates:
-
Display Current Date:
Get-Date -
Custom Format:
You can format the date using the-Formatparameter:Get-Date -Format "yyyy-MM-dd HH:mm:ss" -
Examples of Custom Formats:
- Full date and time:
Get-Date -Format "dddd, MMMM dd, yyyy" - Short date:
Get-Date -Format "MM/dd/yyyy"
- Full date and time:
Common Format Specifiers in PowerShell:
yyyy: Four-digit yearMM: Two-digit monthdd: Two-digit dayHH: Hour in 24-hour formatmm: Minutesss: Secondsdddd: Full weekday nameMMMM: Full month name
Using PowerShell is generally recommended for more advanced date formatting in Windows.
