Atom editor: snippet for inserting a timestamp

后端 未结 2 1022
滥情空心
滥情空心 2021-01-21 08:34

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

相关标签:
2条回答
  • 2021-01-21 08:49

    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.

    0 讨论(0)
  • 2021-01-21 09:03

    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.

    0 讨论(0)
提交回复
热议问题