3

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.

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?

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
    """
Kyle Needham
  • 3,331
  • 2
  • 22
  • 38
Dan L
  • 3,896
  • 3
  • 30
  • 63

2 Answers2

2

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()

This will print the date like this:

2014-09-05T07:15:14.840Z

The downside is that this will always print the date in UTC.

Some more options are listed here: How to format a JavaScript date - maybe you'll see something there. Based on a quick glance of the answers, what you're doing looks pretty good actually.

Community
  • 1
  • 1
nwinkler
  • 46,078
  • 18
  • 137
  • 157
-3

For using momentjs here is minimal example of a snippet: http://jsfiddle.net/jasdeepkhalsa/a0m9s3rc/

HTML & JavaScript - (index.html)

<!doctype html>
<html>    
    <body>
        <script src="http://momentjs.com/downloads/moment.min.js"></script>
        <script>
            (function (name) {
                var date = moment().format('YYYY-MM-DD HH:MM'),
                    name = name || '< Developer Name >',
                    string = date + ' / ' + name;
                return console.log(string);
            })('Dan L');
        </script>
    </body>
</html>

This outputs into the browser's console:

2014-09-05 15:09 / Dan L

Note: This currently outputs to the browser's F12 developer tool's console using console.log, which you could change to output to the page with document.write in the return statement instead.

Jasdeep Khalsa
  • 5,890
  • 7
  • 35
  • 57
  • This has nothing to do with the requested feature. OP asks for a snippet to insert the timestamp in the Atom editor. Not as Javascript (in an HTML page or whatever). – Kafoso Jan 05 '16 at 19:49