Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merge am/pm support into master

  • Loading branch information...
commit 2b1bc8486737548a66c8b3c5b0352d13ff64406c 2 parents 8b494f0 + 98fb6bb
@zackdever authored
Showing with 59 additions and 7 deletions.
  1. +9 −2 README.md
  2. +23 −0 example.html
  3. +27 −5 time.js
View
11 README.md
@@ -27,7 +27,7 @@ Built for [Promt](http://promtapp.com), to solve [this problem](http://stackover
Examples
--------
-Some example uses with input boxes can be viewed in examples.html.
+Some example uses can be viewed in examples.html.
Parses strings such as "8:20" into a Date-less Time.
@@ -39,11 +39,18 @@ If you fancy it, you can use safely drop the 'new'.
Time('1.23') // 1:23
Time('123') // 1:23
+am/pm can optionally be specified.
+
+ Time('8:30 pm') // 8:30 pm
+ Time('3p') // 3:00 pm
+ Time('3 A.M.') // 3:00 am
+
Converts Time into the next corresponding JavaScript Date.
// assume it's 3:15 pm Aug 10
Time('415').nextDate() // 4:15 pm Aug 10
Time('2').nextDate() // 2:00 am Aug 11
+ Time('2 pm').nextDate() // 2:00 pm Aug 11
Does validation statically...
@@ -61,7 +68,7 @@ Accepts numbers too.
Time(1).isValid() // true
-Periods (am/pm) and military time are not supported, but probably will be soon.
+*Military time is not supported, but may be in the future (or not).*
Test
----
View
23 example.html
@@ -47,10 +47,33 @@
</script>
</p>
+ <!-- nextDate example -->
+ <p id='nextdate'>
+ next date: <input> <button disabled>next date</button>
+ <span></span>
+ <script>
+ var nextDate = function() {
+ var root = document.getElementById('nextdate')
+ , input = root.getElementsByTagName('input')[0]
+ , result = root.getElementsByTagName('span')[0]
+ , submit = root.getElementsByTagName('button')[0];
+
+ submit.onclick = function() {
+ result.innerText = Time(input.value).nextDate();
+ };
+
+ input.onkeyup = function() {
+ submit.disabled = !Time.isValid(input.value);
+ };
+ };
+ </script>
+ </p>
+
<script>
window.onload = function() {
instant();
validation();
+ nextDate();
};
</script>
</body>
View
32 time.js
@@ -1,11 +1,17 @@
(function() {
+ // TODO support military time
+ // TODO make military time optional
+ // TODO make british delineation optional
+ var am = 'am'
+ , pm = 'pm';
+
// play nice with both node.js and browser
if (typeof module !== 'undefined' && module.exports) module.exports = Time;
else window.Time = Time;
- // what you might expect to be a valid time e.g. 2, 2:00, 12:18, 4.23
- Time.re = /^(10|11|12|[1-9])(?::|\.)?([0-5][0-9])?$/;
+ // what you might expect to be a valid time e.g. 2, 2:00a, 12:18, 4.23 p.m.
+ Time.re = /^(10|11|12|[1-9])(?::|\.)?([0-5][0-9])?([ap].?(m.?)?)?$/;
/*
* Time constructor works with(out) 'new'
@@ -23,6 +29,7 @@
if (result) {
this.hours = parseInt(result[1]);
this.minutes = result[2] ? parseInt(result[2]) : 0;
+ this.period = parsePeriod(result[3]);
}
} else {
// set to current time
@@ -30,11 +37,13 @@
, hours = d.getHours();
this.hours = hours > 11 ? hours - 12 : hours;
this.minutes = d.getMinutes();
+ this.period = hours > 11 ? pm : am;
}
}
/*
* Find the next immediate corresponding Date.
+ *
* Assume it's 3:15 pm Aug 10:
* Time('3:15').nextDate() // 3:15 pm Aug 10
* Time('415').nextDate() // 4:15 pm Aug 10
@@ -44,6 +53,7 @@
if (!this.isValid()) return null;
var hours = this.hours === 12 ? 0 : this.hours; // uniformly handle am/pm adjustments
+ if (this.period === pm) hours += 12;
var d = new Date();
d.setHours(hours);
d.setMinutes(this.minutes);
@@ -52,6 +62,11 @@
// if it has already passed, add 12 hours at a time until it's in the future
while (new Date() > d) d.setHours(d.getHours() + 12);
+
+ // make sure we're in the correct period
+ if (d.getHours() > 11 && this.period === am) d.setHours(d.getHours() + 12)
+ else if (d.getHours() < 12 && this.period === pm) d.setHours(d.getHours() + 12)
+
return d;
}
@@ -78,12 +93,14 @@
* @retun hh:mm e.g. 3:00, 12:23, undefined:undefined
*/
function toString(time) {
- var minutes = time.minutes < 10 ? '0' + time.minutes : time.minutes;
- return time.hours + ':' + minutes;
+ var minutes = time.minutes < 10 ? '0' + time.minutes : time.minutes
+ , result = time.hours + ':' + minutes;
+
+ return (time.period == undefined) ? result : result + ' ' + time.period;
}
/*
- * (private) Force @time to a string and remove all whitespace
+ * (private) Force @time to a string and remove all whitespace.
*
* @time input
* @retun input as a string, with all white space removed
@@ -91,4 +108,9 @@
function sanitize(time) {
return time.toString().replace(/\s/g, '');
}
+
+ function parsePeriod(period) {
+ if (!period) return null;
+ return (period.match(/^p/i) == null) ? am : pm;
+ }
})();
Please sign in to comment.
Something went wrong with that request. Please try again.