开发者

Formatting DateTime in VIM without leading zeroes on Month, Day, and Hour

开发者 https://www.devze.com 2023-01-08 01:59 出处:网络
Does anyone know if there\'s a way to format the date generated by strftime in Vim (under MS Windows) such that Month, Day, and Hour are no开发者_开发问答t padded to two digits with a leading zero?

Does anyone know if there's a way to format the date generated by strftime in Vim (under MS Windows) such that Month, Day, and Hour are no开发者_开发问答t padded to two digits with a leading zero?

For example, the following commands in vimrc:

nmap <F3> a<C-R>=strftime("%I:%M %p %m/%d/%Y ")<CR><Esc>
imap <F3> <C-R>=strftime("%I:%M %p %m/%d/%Y ")<CR>

will print

03:32 AM 07/09/2010 

into the file, if it was actually July 9th.

I'd like to know if there's a format string that will print

3:32 AM 7/9/2010

for vim instead. I know strftime is platform-specific, so I'm looking for a Windows-specific solution, without the use of external tools. I'd also appreciate comments to the effect that this is impossible with those constraints.

Thanks.


Some operating systems support %k and %e for the single-digit hour and day, respectively. Neither of these seem to be supported on Windows (or not on Windows XP, at least, which is all I've got nearby to try it on).

But all is not lost. Windows does support %x, which gives the date in the 7/9/2010 format you want, and we can get rid of any leading zero with a call to substitute():

echo substitute(strftime("%I:%M %p %x"), "^0", "", "")

This prints

3:50 AM 7/19/2010

Keep in mind that all of this is specific not only to the OS, but also to the current locale. This really isn't portable at all.


I don't know if there are any ways to tell strftime() how to generate what you are looking for. However, you can still apply a substitute(strftime(...), '\<0\+\ze\d', '', 'g').

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号