{"id":1,"date":"2015-07-12T18:43:00","date_gmt":"2015-07-12T17:43:00","guid":{"rendered":"http:\/\/www.gts-ltd.co.uk\/wordpress\/?p=1"},"modified":"2015-12-25T14:39:28","modified_gmt":"2015-12-25T14:39:28","slug":"hello-world","status":"publish","type":"post","link":"https:\/\/www.gts-ltd.co.uk\/wordpress\/2015\/07\/12\/hello-world\/","title":{"rendered":"Using Bootstrap3 Datetimepicker in Meteor"},"content":{"rendered":"<p>I&#8217;ve just started using a platform\u00a0called <a href=\"https:\/\/www.meteor.com\/\" target=\"_blank\">Meteor<\/a> to develop web and android apps using JavaScript. So far I&#8217;ve found it remarkably well thought out, powerful and easy to use, but there are a few areas where it&#8217;s taken me longer to work out how to use a feature.<\/p>\n<p>One such is the <a href=\"https:\/\/github.com\/tsega\/meteor-bootstrap3-datetimepicker\/\" target=\"_blank\">Bootstrap3 Datetimepicker<\/a>. It has <a href=\"http:\/\/eonasdan.github.io\/bootstrap-datetimepicker\/\" target=\"_blank\">extensive documentation<\/a> but I found it tricky\u00a0to install and use it under Meteor.<\/p>\n<p>So here is a step by step tutorial in case anybody else is having similar problems. I assume that readers have installed Meteor and done at least one <a href=\"https:\/\/www.meteor.com\/tutorials\/blaze\/creating-an-app\" target=\"_blank\">Meteor tutorial<\/a>.<!--more--><\/p>\n<p>I&#8217;m running Meteor version 1.1.0.2 under Linux Mint 17.1 and these instructions work as of 14th July 2015.<\/p>\n<h2>Create a Meteor project<\/h2>\n<p>You will need to create a project that includes jQuery, Bootstrap, and the Datetimepicker itself. To do this, open a terminal window and navigate into your Meteor directory. Run the following commands:<\/p>\n<pre><code>meteor create datetimepicker\r\ncd datetimepicker\r\nmeteor add jquery\r\nmeteor add twbs:bootstrap\r\nmeteor add tsega:bootstrap3-datetimepicker<\/code><\/pre>\n<h2>Basic use of Datetimepicker<\/h2>\n<p>First, let&#8217;s\u00a0include the Datetimepicker with its default settings and display the date that the user selects. When the user makes a selection, we use &#8220;Session&#8221; to store the date, formatted as a string.<\/p>\n<p>Replace the contents of datetimepicker.html with this code:<\/p>\n<pre>&lt;head&gt;\r\n &lt;title&gt;datetimepicker&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n &lt;h1&gt;Bootstrap3 Datetimepicker in Meteor&lt;\/h1&gt;\r\n{{&gt; say_when}}\r\n&lt;\/body&gt;\r\n&lt;template name=\"say_when\"&gt;\r\n &lt;div class=\"input-group datetimepicker\"&gt;\r\n &lt;input class=\"set-due-date form-control\" type=\"text\"\/&gt;\r\n &lt;span class=\"input-group-addon\"&gt;&lt;i class=\"glyphicon glyphicon-calendar\"&gt;&lt;\/i&gt;&lt;\/span&gt;\r\n &lt;\/div&gt;\r\n&gt;&lt;p&gt;{{show_date}}&lt;\/p&gt;\r\n&lt;\/template&gt;<\/pre>\n<p>And replace the contents of datetimepicker.js with this code:<\/p>\n<pre>if (Meteor.isClient) {\r\n  Template.say_when.onRendered(function() {\r\n    this.$('.datetimepicker').datetimepicker({\r\n    }).on('dp.change', function(e){\r\n      Session.set(\"selected\", e.date.format());\r\n    });\r\n  });\r\n  Template.say_when.helpers({\r\n    show_date: function(){\r\n      return Session.get(\"selected\");\r\n    }\r\n  });\r\n}<\/pre>\n<h2>Set some options<\/h2>\n<p>You can use <a href=\"http:\/\/eonasdan.github.io\/bootstrap-datetimepicker\/Options\" target=\"_blank\">Datetimepicker&#8217;s options<\/a> to adjust how your Datetimepicker behaves. Use the options shown below to:<\/p>\n<ul>\n<li>Show the current date and time when the page loads<\/li>\n<li>Avoid opening the keyboard on mobile devices when the user opens the picker.<\/li>\n<\/ul>\n<p>Replace the definition of the datetimepicker in datetimepicker.js with this code:<\/p>\n<pre>Template.say_when.onRendered(function() {\r\n  this.$('.datetimepicker').datetimepicker({\r\n    defaultDate: new Date(), \/\/ show the current date and time in the input\r\n    focusOnShow: false, \/\/ prevent the keyboard from opening when you open the picker on mobile devices\r\n  }).on('dp.change', function(e){\r\n    Session.set(\"selected\", e.date.format());\r\n  });\r\n});<\/pre>\n<h2>Use &#8216;format&#8217; to set date and not time<\/h2>\n<p>The &#8216;format&#8217; option is very powerful, allowing you to change not only how time and date are displayed but also whether time and date can be selected. See the <a href=\"http:\/\/momentjs.com\/docs\/#\/displaying\/format\/\" target=\"_blank\">moment documentation<\/a> for a list of all valid formats.<\/p>\n<p>Note that the same format should be used for saving the selected date \/ time to Session.<\/p>\n<p>For example, to show date only in the form &#8220;Jul\/14\/2015&#8221; use this code in your datetimepicker.js:<\/p>\n<pre>Template.say_when.onRendered(function() {\r\n  this.$('.datetimepicker').datetimepicker({\r\n    defaultDate: new Date(), \/\/ show the current date and time in the input\r\n    focusOnShow: false, \/\/ prevent the keyboard from opening when you open the picker on mobile devices\r\n    format: 'MMM\/D\/YYYY' \/\/ only show date picker\r\n  }).on('dp.change', function(e){\r\n    Session.set(\"selected\", e.date.format('MMM\/D\/YYYY')); \/\/ only save date\r\n  });\r\n});<\/pre>\n<h2>Set time only and not date<\/h2>\n<p>To use Datetimepicker to pick time only:<\/p>\n<ul>\n<li>Use the &#8216;format&#8217; option to restrict the picker to time only.<\/li>\n<li>Use format() to only save time to Session.<\/li>\n<li>Change the icon from calendar to clock.<\/li>\n<\/ul>\n<p>Revised\u00a0code for datetimepicker.js:<\/p>\n<pre>if (Meteor.isClient) {\r\n  Template.say_when.onRendered(function() {\r\n    this.$('.datetimepicker').datetimepicker({\r\n    defaultDate: new Date(), \/\/ show the current date and time in the input\r\n    focusOnShow: false, \/\/ prevent the keyboard from opening when you open the picker on mobile devices\r\n    format: 'HH:mm a' \/\/ only show time picker, using 12-hour clock\r\n   }).on('dp.change', function(e){\r\n     Session.set(\"selected\", e.date.format('HH:mm a')); \/\/ only save time, not date\r\n   });\r\n  });\r\n  Template.say_when.helpers({\r\n    show_date: function(){\r\n    return Session.get(\"selected\");\r\n  }\r\n });\r\n}<\/pre>\n<p>Revised code for datetimepicker.html:<\/p>\n<pre>&lt;head&gt;\r\n &lt;title&gt;datetimepicker&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n &lt;h1&gt;Bootstrap3 Datetimepicker in Meteor&lt;\/h1&gt;\r\n {{&gt; say_when}}\r\n&lt;\/body&gt;\r\n&lt;template name=\"say_when\"&gt;\r\n &lt;div class=\"input-group datetimepicker\"&gt;\r\n &lt;input class=\"set-due-date form-control\" type=\"text\"\/&gt;\r\n &lt;span class=\"input-group-addon\"&gt;&lt;i class=\"glyphicon glyphicon-time\"&gt;&lt;\/i&gt;&lt;\/span&gt;\r\n &lt;\/div&gt;\r\n\r\n &lt;p&gt;{{show_date}}&lt;\/p&gt;\r\n&lt;\/template&gt;<\/pre>\n<h2>Note on icons<\/h2>\n<p>The <a href=\"https:\/\/github.com\/tsega\/meteor-bootstrap3-datetimepicker\/\" target=\"_blank\">author says to use the code below<\/a> to include the Datetimepicker. However I found that this didn&#8217;t show any icon on the element you need to click to open the calendar.<\/p>\n<pre>&lt;<span class=\"pl-ent\">div<\/span> <span class=\"pl-e\">class<\/span>=<span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>input-group datetimepicker<span class=\"pl-pds\">\"<\/span><\/span>&gt;\r\n    &lt;<span class=\"pl-ent\">span<\/span> <span class=\"pl-e\">class<\/span>=<span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>input-group-addon<span class=\"pl-pds\">\"<\/span><\/span>&gt;&lt;<span class=\"pl-ent\">i<\/span> <span class=\"pl-e\">class<\/span>=<span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>fa fa-calendar<span class=\"pl-pds\">\"<\/span><\/span>&gt;&lt;\/<span class=\"pl-ent\">i<\/span>&gt;&lt;\/<span class=\"pl-ent\">span<\/span>&gt;\r\n    &lt;<span class=\"pl-ent\">input<\/span> <span class=\"pl-e\">class<\/span>=<span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>set-due-date form-control<span class=\"pl-pds\">\"<\/span><\/span> <span class=\"pl-e\">type<\/span>=<span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>text<span class=\"pl-pds\">\"<\/span><\/span>\/&gt;\r\n  &lt;\/<span class=\"pl-ent\">div<\/span>&gt;<\/pre>\n<p>The <a href=\"http:\/\/eonasdan.github.io\/bootstrap-datetimepicker\/\" target=\"_blank\">Bootstrap documentation<\/a> led me to use this code instead:<br \/>\n<code><\/code><\/p>\n<pre>&lt;div class=\"input-group datetimepicker\"&gt;\r\n  &lt;input class=\"set-due-date form-control\" type=\"text\"\/&gt;\r\n  &lt;span class=\"input-group-addon\"&gt;&lt;i class=\"glyphicon glyphicon-calendar\"&gt;&lt;\/i&gt;&lt;\/span&gt;\r\n &lt;\/div&gt;<\/pre>\n<p>The span &#8220;input-group-addon&#8221; has been moved to after the input, and the tag inside has the class &#8220;glyphicon glyphicon-calendar&#8221;.<\/p>\n<p>With this code, I see the calendar icon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just started using a platform\u00a0called Meteor to develop web and android apps using JavaScript. So far I&#8217;ve found it remarkably well thought out, powerful and easy to use, but there are a few areas where it&#8217;s taken me longer to work out how to use a feature. One such is the Bootstrap3 Datetimepicker. It &hellip; <a href=\"https:\/\/www.gts-ltd.co.uk\/wordpress\/2015\/07\/12\/hello-world\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Bootstrap3 Datetimepicker in Meteor<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[4,5,2,3],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-bootstrap","tag-datetimepicker","tag-meteor","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":10,"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/1\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gts-ltd.co.uk\/wordpress\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}