How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?

前端 未结 28 2089
别跟我提以往
别跟我提以往 2020-11-21 05:28

Update: Now that it\'s 2016 I\'d use PowerShell for this unless there\'s a really compelling backwards-compatible reason for it, particularly because of the regional setting

28条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 05:57

    I know that there are numerous ways mentioned already. But here is my way to break it down to understand how it is done. Hopefully, it is helpful for someone who like step by step method.

    :: Check your local date format
    echo %date%
    
        :: Output is Mon 08/15/2016
    
    :: get day (start index, number of characters)
    ::         (index starts with zero)
    set myday=%DATE:~0,4%
    echo %myday%
        :: output is Mon 
    
    :: get month
    set mymonth=%DATE:~4,2%
    echo %mymonth%
        :: output is 08
    
    :: get date 
    set mydate=%DATE:~7,2% 
    echo %mydate%
        :: output is 15
    
    :: get year
    set myyear=%DATE:~10,4%
    echo %myyear%
        :: output is 2016
    

提交回复
热议问题