Skip to content

Commit

Permalink
merge am/pm support into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zackdever committed Sep 16, 2012
2 parents 8b494f0 + 98fb6bb commit 2b1bc84
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -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.

Expand All @@ -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...

Expand All @@ -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
----
Expand Down
23 changes: 23 additions & 0 deletions example.html
Expand Up @@ -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>
Expand Down
32 changes: 27 additions & 5 deletions 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'
Expand All @@ -23,18 +29,21 @@
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
var d = new Date()
, 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
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -78,17 +93,24 @@
* @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
*/
function sanitize(time) {
return time.toString().replace(/\s/g, '');
}

function parsePeriod(period) {
if (!period) return null;
return (period.match(/^p/i) == null) ? am : pm;
}
})();

0 comments on commit 2b1bc84

Please sign in to comment.