Skip to content

Instantly share code, notes, and snippets.

@sieste
Created April 9, 2018 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sieste/932e81ba9d2eb9babf2f40496d0a80dd to your computer and use it in GitHub Desktop.
Save sieste/932e81ba9d2eb9babf2f40496d0a80dd to your computer and use it in GitHub Desktop.
vimscript function to format a string that describes a date ('now', 'in 2 days 2pm', 'next week'), using pythons `dateparser` module
" vimscript function takes input string `inp`, hands it to python function `parse` (module `dateparser`),
" and outputs the parsed date in format `fmt`; return `inp` if string could not be parsed to valid date
" Example:
" :echo ParseDate('tomorrow', '%Y %m %d')
function! ParseDate(inp, fmt)
python << endpython
import vim, dateparser as dp
inp = vim.eval('a:inp')
fmt = vim.eval('a:fmt')
inp_parsed = dp.parse(inp, languages=['en','de'],
settings={'PREFER_DATES_FROM': 'future'})
out = inp
if type(inp_parsed).__name__ == 'datetime':
out = inp_parsed.strftime(fmt)
vim.command("let s:ans = '%s'" % out)
endpython
return s:ans
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment