Search code examples
windowscmdline

How to get date and time stamp in format dd-mm-yyyy hh:mm:ss format in windows command prompt


Can any one please provide me a solution as to: a. how i create a variable in windows CMD line and store the date and time in dd-mm-yyyy hh:mm:ss format b. Also Please let me know how to find difference between two datetime's of the above format in windows command line


Solution

  • There are various ways, but a python Programmer I would write a short script, so that I can fit the output to my use case.

    import time import datetime print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')

    Then you just need to execute the python script.

    Under Windows:

    c:\path\to\python3.exe script_name.py

    and then it should return your desired timestamp.

    The other option is, to create a date-time variable.

    set datetime1=%date:~4,2%-%date:~7,2%-%date:~-4% %time:~0,2%:%time:~3,2%:%time:~6,2%

    this is callable with:

    echo %datetime

    Output: 01-03-2020 15:00:14