How to get timestamp in VS code task?

Viewed 2720

I'm running small shell commands via tasks in VS code.

Within these commands I need the current timestamp.

Given I'm trying to run something like mkdir ./%date in a task:

{
    "label": "make timestamp dir",
    "type": "shell",
    "command": "mkdir ${workspaceFolder}/%date%",
},

I tried using %date% and %time% but both is not substituted.

Alternatively I did see that VS code allows to call commands with ${command:CommandID}. Therefore I installed a extension that generates a timestamp: https://marketplace.visualstudio.com/items?itemName=jsynowiec.vscode-insertdatestring I tried to use that with ${command:insertDateString.insertTimestamp}:

{
    "label": "make timestamp dir",
    "type": "shell",
    "command": "mkdir ${workspaceFolder}/${command:insertDateString.insertTimestamp}",
},

But that also is not substituted.

I'm out of ideas. How to add a timestamp in a VS code task?

2 Answers

In arguments section I used forward slash (after two backslashes) as an escape char so, that following created me a folder with timestamp:

"args": ["${workspaceFolder}\\/%date:~-4%%date:~-7,2%%date:~-10,2%_%time:~0,2%%time:~3,2%%time:~6,2%"]

You could try:

  "command": "mkdir -p ${workspaceFolder}/`date +%Y%m%d-%H%M`",

That bash command date works for me. I assume you are using a unix-type shell and that'll work. -p forces it to create the new folder if it doesn't already exist.

See also in vscode how can I quickly generate a new file with datetime in the name?

[I don't know that you want ${workspaceFolder} -that appears to be a full path, maybe you want ${workspaceFolderBasename} but that is a separate issue.]

Related