76

Does anyone know of a way that I can insert the current date & time in a visual studio code by snippets?

I have looked docs but did not get any information about it.

I want to create a snippets like this:

title: theFileTitle
date: 2016-08-05 09:44:16
ACBingo
  • 1,078
  • 1
  • 8
  • 13

9 Answers9

85

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",
    ]
}

Type head, press ctrl+space and it should show snippet menu.

ccpizza
  • 28,968
  • 18
  • 162
  • 169
LukeBatchelor
  • 951
  • 6
  • 6
  • 2
    FYI if you don't see the time it is because you have to remove the new line between the date and time formatting. – h3dkandi Feb 27 '19 at 12:00
  • @Robert This is not necessarily better than the accepted solution. While a "native" solution is preferred when available, it lacks some functionality -- for example, I needed the three-letter day name, which is not possible using this method. – Jan Pokorný Sep 17 '21 at 08:27
  • Neither was it the OPs question, right? Referring to the actual question, I prefer this answer over the accepted one. Still the accepted one is absolutely right and apparently still convinced the majority of readers. I'm happy seeing both ways here - wonderful free world ;) – Robert Oct 07 '21 at 13:09
74

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.

enter image description here

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!!

attomos
  • 1,112
  • 3
  • 16
  • 30
Jakub Synowiec
  • 5,719
  • 1
  • 29
  • 37
  • Sadly, CTRL + SHIFT + I conflicts with VSCode code formatting. – Frederik Krautwald Oct 02 '17 at 18:12
  • @FrederikKrautwald you can always remap it or use through command palette. `⌃+⇧+I` was not present on the Windows keybindings list when I published it. – Jakub Synowiec Oct 20 '17 at 05:33
  • Related info: The extension vscode-fileheader inserts author name, creation date, modification date etc. https://marketplace.visualstudio.com/items?itemName=mikey.vscode-fileheader – vineeshvs May 03 '20 at 07:03
  • Where do you edit the shortcut key for this? – not2qubit Jan 25 '22 at 02:02
  • It seems that this extension could be deprecated due to built-in [snippet variables](https://code.visualstudio.com/updates/v1_20#_more-snippet-variables) functionality mentioned by other people here. – pjboro Jun 26 '22 at 10:30
58

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.

jianyongli
  • 1,131
  • 12
  • 10
6

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

fst1
  • 97
  • 1
  • 4
3

The env variable TM_FILENAME will fill the title with the file name automatically.

For example:

"title: ${1:$TM_FILENAME_BASE}"
James Skemp
  • 8,018
  • 9
  • 64
  • 107
1

You could also use a tool outside of Code, like for example Texter. I have configured it to replace [t with [%ds %t], which gives me [9/11/2017 16:30] while I type, regardless of application.

1

See https://stackoverflow.com/a/74082031/836330 for how to insert the Intl.DateTimeFormat timeStamp with options with a keybinding:

adding a time stamp with padding


Or see Command Variable extension. It uses the Intl.DateTimeFormat format and can be used in a keybinding like so:

 {
    "key": "alt+d",
    "when": "editorTextFocus",
    "command": "extension.commandvariable.dateTimeInEditor",

    "args": {
      "locale": "en-US",
      "options": {
        "year": "numeric",
        "month": "long",
        "weekday": "long",
        "day": "2-digit",
        "hour12": false,
        "hour": "2-digit",
        "minute": "2-digit",
        "second": "2-digit"
      },
      "template": "${month} ${day}, (${weekday}), ${year} - ${hour}:${minute}::${second}"
    }
  },

to produce

March 25, (Wednesday), 2020 - 21:16::49

and many other timestamp versions. See the possibilities at Intl.DateTimeFormat

Mark
  • 143,421
  • 24
  • 428
  • 436
0

It might be a bit of an overkill but you can check Org Mode extension which has this functionality and more:

Org Mode change date gif

vdi
  • 743
  • 10
  • 20
0

You can use this snippet for Vscode, I used to refer to this in my code files.


"file description": {
  "prefix": "template",
  "body": [
    "\"\"\"",
    "# _* coding: utf8 *_",
    "",
    "filename: $TM_FILENAME",
    "",
    "@author: sounishnath",
    "createdAt: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
    "\"\"\"",
    "",
    ""
  ],
  "description": "file description"
}

sounish nath
  • 567
  • 4
  • 3