Atom编辑器:用于插入时间戳的代码段 [英] Atom editor: snippet for inserting a timestamp
问题描述
以下是我正在玩的Atom代码段。我想做的是在开发人员的名字的末尾插入一个时间戳。当多个人在同一个代码库上工作,并且需要注释掉一些代码或添加注释时,这是非常有用的。这样其他开发人员知道谁做了什么,当他们做到了。我发现它非常有用,想要创建一个代码片段来做到这一点。
Below is an Atom snippet I was playing with. What I want to do is insert a timestamp with a developer's name at the end. This is useful when multiple folks are working on the same codebase and you need to comment out some code or add a comment. This way other developers know who did what, and when they did it. I've found it very useful and wanted to create a snippet to do this.
但是,正如你可以看到从代码片段,这是非常丑陋... JS是不是我的forte。这是否有干净的方法?
However, as you can see from the snippet, it's very ugly...JS is not my forte. Is there a clean way to do this?
时间
=> tab => YYYY -MM-DD HH:MM / NAME
time
=> tab => YYYY-MM-DD HH:MM / NAME
'.source':
'Timestamp':
'prefix': 'time'
'body': """
# #{datetime = new Date(); datetime.getFullYear()}-#{(datetime.getMonth()+1)}-#{datetime.getDate()} #{datetime.getHours()}:#{datetime.getMinutes()} / NAME
"""
推荐答案
无论如何使用moment.js Date.js使用 toISOString()
The closest you'll get to this without resorting to a library like moment.js or Date.js is by using toISOString()
new Date().toISOString()
这将打印如下日期:
2014-09-05T07:15:14.840Z
缺点是总是以UTC打印日期。
The downside is that this will always print the date in UTC.
此处列出了一些其他选项:如何格式化javascript日期 - 也许你会看到的东西。基于对答案的快速浏览,你在做什么看起来很不错。
Some more options are listed here: how to format javascript date - maybe you'll see something there. Based on a quick glance of the answers, what you're doing looks pretty good actually.
这篇关于Atom编辑器:用于插入时间戳的代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!