How to format date in Windows?

088

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

  1. Display Current Date:

    date /T
  2. 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-%b

    This 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:

  1. Display Current Date:

    Get-Date
  2. Custom Format:
    You can format the date using the -Format parameter:

    Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  3. Examples of Custom Formats:

    • Full date and time:
      Get-Date -Format "dddd, MMMM dd, yyyy"
    • Short date:
      Get-Date -Format "MM/dd/yyyy"

Common Format Specifiers in PowerShell:

  • yyyy: Four-digit year
  • MM: Two-digit month
  • dd: Two-digit day
  • HH: Hour in 24-hour format
  • mm: Minutes
  • ss: Seconds
  • dddd: Full weekday name
  • MMMM: Full month name

Using PowerShell is generally recommended for more advanced date formatting in Windows.

0 Comments

no data
Be the first to share your comment!