Saturday, 14 July 2012

Moment.js




---------------------------------



<!-- <html>
  <head>
    <title>in a Moment.js</title>
  </head>

  <body>
    <h1>Moment.js here now: <span id="then" data-date="Sat Jul 14 2012 07:43:14 GMT-0400 (EDT)"></span></h1>
   
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="https://raw.github.com/timrwood/moment/1.6.2/moment.js"></script>
    <script>
      $(document).ready(function(){
        var then = $('#then'),
            date = moment(new Date(then.attr('data-date'))),
            update = function(){
                       then.html(date.fromNow());
                      };
       

        update();
        setInterval(update, 6000);
      });
    </script>
  </body>

</html>



 -->




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Formatting dates using the Moment.js date library</title>
</head>
<body>

    <p><time data-momentjs>2012-07-12</time></p>
    <p><time data-momentjs>1976-09-09</time></p>

    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script type="text/javascript" src="https://raw.github.com/timrwood/moment/1.1.1/moment.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function ($) {
            $("time[data-momentjs]").each(function (idx, item) {
                var $item = $(item),
                    mdate = moment($item.text(), "YYYY-MM-DD");
                $item.attr("title", mdate.format("ddd, MMM Do, YYYY")).text(mdate.fromNow());
            });
        });
    </script>

</body>
</html>



// node:
var moment = require('moment');
moment().add('days', 2).fromNow();
// 'in 2 days'
moment().subtract('days', 2).fromNow();
// '2 days ago'
moment('November 1977').fromNow()
// '34 years ago'
moment().add('days', 2).calendar();
// 'Monday at 8:30 AM'
moment().subtract('days', 2).calendar();
// 'last Thursday at 8:30 AM'
moment('1977-08-20 14:29:00 UTC').format('MMM. d, YYYY');
// 'Aug. 5, 1977'
moment('1977-08-20 14:29:00 UTC').fromNow();
// 'il y a 35 années'
<html>
<head>
<title>in a Moment.js</title>
</head>
<body>
<h1>Moment.js here now: <span id="then" data-date="Sat Mar 24 2012 08:42:14 GMT-0400 (EDT)"></span></h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/js/moment.js"></script>
<script>
$(document).ready(function(){
var then = $('#then'),
date = moment(new Date(then.attr('data-date'))),
update = function(){
then.html(date.fromNow());
};
update();
setInterval(update, 60000);
});
</script>
</body>
</html>
------------------------------ in a Moment.js

Moment.js here now:

Formatting dates using the Moment.js date library


No comments:

Post a Comment