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?


Edit 1: Thank you much for incorporating this feature. Its so nice that every time a create a new timestamp, on Nov. 5th and beyond, it automatically shows -08:00 instead of -07:00... ah, small win!

6
  • Please upvote this request in the VS Code Github repo. The last two requests "didn't receive the minimum 20 upvotes" needed to add this to the backlog! Commented Jun 3, 2022 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. Commented Jun 3, 2022 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! Commented Jun 3, 2022 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). Commented Jun 3, 2022 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? Commented Jun 4, 2022 at 21:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.