Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Converts a well-formed string representing a time of day into a JavaScript date object. */
- function Time(input){
- var match, date, hours, minutes, seconds, milliseconds;
- /**
- * Override the returned Date object's toString method to return a numeric value, unless an argument is explicitly passed
- * requesting the date's representation as a human-readable string. To invoke the standard behaviour of Date.toString,
- * consider using the instance's toLocaleString or toUTCString methods instead.
- *
- * @param {bool} Returns a string formatted to 12 or 24-hour time if set to FALSE or TRUE, respectively.
- * @return {int|string} The number of milliseconds of the generated date if in24hr is undefined; a string otherwise.
- */
- var toString = function(in24hr){
- if(undefined === in24hr) return this.getTime();
- var h = this.getUTCHours(),
- m = this.getUTCMinutes(),
- s = this.getUTCSeconds() || "",
- ms = this.getUTCMilliseconds() || "";
- if(ms){
- if(ms < 10) ms = "00" + ms;
- else if(ms < 100) ms = "0" + ms;
- }
- return (in24hr ? ((h < 10 ? "0" : "")+h) : (!h ? 12 : (h > 12 ? h-12 : h))) + ":"
- + (m < 10 ? "0" : "") + m
- + (s? ":"+(s+(ms? "."+ms:"")) : "")
- + (in24hr ? "" : (h < 12 ? " am" : " pm"));
- };
- /** Allow function to be called without an argument; will default to current time. */
- if(undefined === input) input = "now";
- if((match = input.match(/^\s*(\d+)(?::(\d{2})\s*(?::(\d{2})(\.\d+)?)?)?\s*((?:a|p)\.?m\.?)?\s*$/i))){
- hours = parseInt(match[1]);
- minutes = parseInt(match[2]);
- seconds = parseInt(match[3]);
- milliseconds = match[4] ? Math.round((parseFloat(match[4]) * 1000)) : 0;
- var meridiem = !match[5] ? undefined : (match[5][0].toLowerCase() === "p" ? "PM" : "AM");
- if(meridiem === "PM" && hours < 12)
- hours += 12;
- /** Don't be fooled: 12pm is technically kinda 0pm. */
- else if(meridiem === "AM" && hours == 12)
- hours = 0;
- /** Generate our returned value. */
- date = new Date(0);
- date.toString = toString;
- date.inputString = input;
- date.setUTCHours(hours);
- date.setUTCMinutes(minutes || 0);
- date.setUTCSeconds(seconds || 0);
- date.setUTCMilliseconds(milliseconds || 0);
- return date;
- }
- /** Allow some common shorthand values to be passed in instead. */
- switch(input.toLowerCase()){
- case "now":{
- date = new Date();
- date.toString = toString;
- date.inputString = input;
- date.setUTCFullYear(1970);
- date.setUTCMonth(0);
- date.setUTCDate(1);
- return date;
- break;
- }
- case "midnight":{
- date = new Date(0);
- date.toString = toString;
- date.inputString = input;
- return date;
- break;
- }
- case "midday":
- case "noon":{
- date = new Date(43200000);
- date.toString = toString;
- date.inputString = input;
- return date;
- break;
- }
- }
- /** Otherwise, we weren't supplied a value that we could read. Return NULL. */
- return null;
- };
- /**
- * Measures the number of milliseconds between a set of times.
- *
- * @param {string} input A comma-separated sequence of hyphen-delimited time ranges ("7:30am - 9:30am, 11:30am - 12:15pm")
- * @return {int} The total number of milliseconds between each of the supplied times.
- */
- Time.between = function(input){
- var rSplit = /\s*-\s*/g,
- ranges = input.split(/,/g),
- output = 0;
- var rHour = /^\s*\d+/,
- rMeridiem = /((?:a|p)\.?m\.?)\s*$/i;
- var range, i, from, to, fromHour, toHour, match;
- for(i = 0; i < ranges.length; ++i){
- range = ranges[i].split(rSplit);
- from = range[0];
- to = range[range.length-1];
- /** Only the first time was supplied an AM/PM suffix. */
- if(!rMeridiem.test(from) && (match = to.match(rMeridiem))){
- fromHour = parseInt(from.match(rHour)[0]);
- toHour = parseInt(to.match(rHour)[0]);
- /** Beware of that bloody 12:00 AM/PM crossover. */
- if(fromHour !== 12 && toHour === 12 && match[1])
- from += "a" === match[1][0].toLowerCase() ? " pm" : " am";
- else
- from += match[1];
- }
- from = Time(from);
- to = Time(to);
- /** Time ranges spanning midnight need to be handled carefully. */
- if(+from >= 43200000 && to < 43200000)
- output += (86400000 - from) + to;
- /** Otherwise, just add the time distance normally. */
- else output += Math.abs(to - from);
- }
- return output;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement