1

I see the time/date variables available to VS Code User Defined Snippets are:

CURRENT_YEAR The current year
CURRENT_YEAR_SHORT The current year's last two digits
CURRENT_MONTH The month as two digits (example '02')
CURRENT_MONTH_NAME The full name of the month (example 'July')
CURRENT_MONTH_NAME_SHORT The short name of the month (example 'Jul')
CURRENT_DATE The day of the month as two digits (example '08')
CURRENT_DAY_NAME The name of day (example 'Monday')
CURRENT_DAY_NAME_SHORT The short name of the day (example 'Mon')
CURRENT_HOUR The current hour in 24-hour clock format
CURRENT_MINUTE The current minute as two digits
CURRENT_SECOND The current second as two digits
CURRENT_SECONDS_UNIX The number of seconds since the Unix epoch

Unfortunately, without TIMEZONE or TIMEZONE OFFSET from UTC, you can't automatically create an accurate, time-aware timestamp using these variables.

Example:

${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}T${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}

Evaluates to: 2022-06-03T07:32:09, but thats not accurate enough. Without a TIMEZONE identifier, that "timestamp" is +/- 23:59:59 reflection of "actual time".

I need:

2022-06-03T07:32:09-07:00 and I want the timezone offset to adjust for daylight savings time to 2022-11-04T07:32:09-08:00 as appropriate.

Must be human readable. Not going to use Unix time.

How can I do this without spinning up a whole Extension?

SeaDude
  • 3,725
  • 6
  • 31
  • 68
  • Please [upvote this request in the VS Code Github repo](https://github.com/microsoft/vscode/issues/151220). The last [two](https://github.com/microsoft/vscode/issues/151220) [requests](https://github.com/microsoft/vscode/issues/120715) "didn't receive the minimum 20 upvotes" needed to add this to the backlog! – SeaDude Jun 03 '22 at 14:51
  • I agree that this would be useful and should be upvoted on GitHub, but I'm not sure what you expect here on StackOverflow? It doesn't appear there is any functionality for custom *variables* in these snippets. – Matt Johnson-Pint Jun 03 '22 at 18:24
  • I added some details to that GitHub issue that would help someone implement. I'm sure they would take a PR if you have the time! – Matt Johnson-Pint Jun 03 '22 at 18:33
  • Thanks @MattJohnson-Pint. What I was hoping for here on SO, is a way to do this with some other workaround until the fix is implemented. Been researching VS Code Tasks as a potential way to do this using Python (my LOC). – SeaDude Jun 03 '22 at 18:41
  • There are extensions that can do this now, as long as `Intl.DateTimeFormat` covers your cases. But you don't want those? – Mark Jun 04 '22 at 21:47
  • PLEASE upvote (give it a thumbs up emoji) this VS Code Github issue now! This is now an enhancement candidate. It needs only 16 more upvotes. https://github.com/microsoft/vscode/issues/151220 – SeaDude Aug 23 '22 at 22:01

2 Answers2

1

I came here looking for the same thing. Its not a snippet, but I use the Insert Date String extension to handle this. You can define the default format as iso, and it comes with a key binding of ctrl-shift-alt-I. Works well enough, but I too would prefer a snippet.

Chad Lowe
  • 721
  • 1
  • 6
  • 12
  • It looks like a `CURRENT_TZOFFSET` or the like *should* be coming to VS Code. The issue I raised (and we all upvoted) made the dev backlog! – SeaDude Sep 27 '22 at 03:11
0

Indeed, coming to vscode v1.78 is a timezone offset variable, see new snippet variable for timezone_offset.

New snippet variable for timezone offset

A new snippet variable, CURRENT_TIMEZONE_OFFSET, is now available. This variable returns the current timezone offset in the format +HHMM or -HHMM (for example -0700).

Mark
  • 143,421
  • 24
  • 428
  • 436
  • note that you can further edit the timezone however you'd like with the ability they give you to regex-search-replace the resulting variable expansion. – 8c6b5df0d16ade6c Jun 27 '23 at 00:49