Atom editor: snippet to insert timestamp

Below is a snippet of Atom I played with. I want to add a timestamp with the developer's name at the end. This is useful when several people are working on the same codebase and you need to comment on some code or add a comment. This way, other developers know who did what, and when they did it. I found this very useful and wanted to create a snippet to do this.

However, as you can see from the snippet, this is 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
    """

      

+3


source to share


2 answers


The closest you can get to this is without resorting to a library like moment.js or Date.js 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 additional options are listed below: How to format the date in JavaScript - you might see something in there. Based on a quick glance at the answers, what you are doing looks pretty good actually.

+2


source


A momentjs

minimal snippet example is provided here for use : http://jsfiddle.net/jasdeepkhalsa/a0m9s3rc/

HTML and 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 is output to the browser console

:

2014-09-05 15:09 / Dan L

      

Note. It is currently displayed in the F12 browser developer toolbar with a help console.log

that you can change to display on a page using document.write

the instructions return

.

-3


source







All Articles