DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Generate Random Date After a Specified Date


The following codes can display a random date within a month after the specified fair date.

$ meteor create randomDate
$ cd randomDate
$ meteor add momentjs:moment
$ cat randomDate.html
<head>
  <title>Random Date</title>
</head>

<body>
  在 展会日期 输入框中输入日期,例如:2015-05-25,然后点击 评论 按钮。
  <hr>
  {{> DatePanel}}
</body>

<template name="DatePanel">
  <label>展会日期:</label>
  <input type="text" id="fairDate">
  <button>评论</button>
  <p>评论添加日期:{{commentDate}}</p>
</template>

$ cat randomDate.js
if (Meteor.isClient) {
  Template.DatePanel.helpers({
    commentDate: function () {
      return Session.get('commentDate');
    }
  });

  Template.DatePanel.events({
    'click button': function (e, t) {
      var dateStr = t.find("#fairDate").value;
      var fairDate = moment(dateStr);
      var randomDays = Math.floor((Math.random() * 30) + 1);
      var commentDate = fairDate.add(randomDays, 'days');
      var commStr = commentDate.format("YYYY-MM-DD");
      Session.set('commentDate', commStr);
    }
  });
}

$ meteor


Published

Sep 20, 2015

Last Updated

Sep 20, 2015

Category

Tech

Tags

  • date 2
  • JavaScript 11
  • Meteor 47
  • moment.js 1

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor