Yann Moisan
Yann Moisan

Reputation: 8281

How to convert a {date} to {time} in pure Vimscript

How to convert a date to time in pure Vimscript.

I have a date (ex: 2013-12-21) and I want to convert it to time in order to call strftime() to get the day of week. Because the 2nd argument of strftime must be a time, as mentioned :h strftime()

Upvotes: 2

Views: 839

Answers (2)

A S
A S

Reputation: 1235

Since version 8.1.2325 (:echom has('patch-8.1.2325')), Vim supports strptime(). Thus, that would be something as easy as this:

:echom strptime('%Y-%m-%d', '2013-12-21')

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172658

Unfortunately, the only sources for {time} (which is the Unix epoch, i.e. seconds since 1-Jan-1970) built-in are localtime() for the current time, and getftime() for a file's modification date.

To convert arbitrary dates, one needs external help; e.g. through an embedded language like Python, or via the date command-line tool. You'll find the latter implemented in ingo#date#epoch#ConvertTo() from my ingo-library plugin.

Upvotes: 3

Related Questions