How to insert current date time in vscode?
Solution 1:
As of Jan 2018 (release 1.20) you can use these new snippet environment variables.
Your example above would look like this:
"File Header": {
"prefix": "header",
"description": "Output a file header with the file name and date",
"body": [
"title: $TM_FILENAME",
"date: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE
$CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
]
}
Solution 2:
I have created an extension for you that allows to insert formatted date and/or time string - Insert Date String.
Installation
Open Command Palette by pressing F1, type ext install
+ press Enter
and then look for Insert Date String extension.
Usage
To insert current date and/or time at the cursor position you can:
Press ⇧+⌘+I (OS X) or Ctrl+Shift+I (Windows and Linux), or open Command Palette by pressing F1 and type Insert DateTime
then press Enter.
Configuration
By default you don't have to set anything. But if you want to change the datetime format, look for insertDateString.format
option in user settings.
// Date format to be used.
"insertDateString.format": "YYYY-MM-DD hh:mm:ss",
You can specify any valid ISO 8601 format. There are some examples in readme.
Snippet
Unfortunately you can't use anything more than tab stops or variables in snippets so you'll have to enter the title and date/time manually.
You can define snippets for specific languages. To open a snippet file for editing, open User Snippets
under File > Preferences (Code > Preferences on Mac OS X)
and select the language for which the snippets should appear.
Following example is for Plain Text files.
After opening a snippet file for Plain Text, add following definition:
{
"File header": {
"prefix": "header",
"body": [
"title: ${title:Enter title}",
"date: ${date:Insert datetime string (⇧⌘I or Ctrl+Shift+I)}"
]
}
}
Now you can open a new plaintext file, enter header
and press Tab
. Enter your title and use Insert DateTime
command to insert current date and/or time.
Idea for a more customizable solution
One could write an extension for inserting such headers. This way some sort of templates with several predefined variables (e.g. date, filename, configurable username/email, etc.) might be used.
Hope this helps!!
Solution 3:
if you don't want create a snippet, there is a simple way, using keybindings.
open keybindings.json (Preferences: Open Keyboard Shortcuts (JSON)), and add fellow code to your keybindings.json
[
{
"key": "cmd+k t",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND"
}
}
]
that's all.
now you can use cmd+k t
to insert current data time while typing.
Solution 4:
You can just use the following variables in your snippets:
$CURRENT_YEAR
$CURRENT_YEAR_SHORT
$CURRENT_MONTH
$CURRENT_DATE
$CURRENT_HOUR
$CURRENT_MINUTE
$CURRENT_SECOND
Links to the official VSCode docs:
date and time in snippets
user defined snippets
Solution 5:
The env variable TM_FILENAME
will fill the title with the file name automatically.
For example:
"title: ${1:$TM_FILENAME_BASE}"