📄 time.js
字号:
// knock off the offset to get the actual UTC hours hour -= isc.Time.UTCHoursOffset; var minutes = date.getUTCMinutes(), seconds = showSeconds ? date.getUTCSeconds() : null, pm = show24 ? null : (hour >=12); // Hour will be in 24 hour format by default if (!show24) { if (hour > 12) hour = hour - 12; if (hour == 0) hour = 12; } if (padded) hour = hour.stringify(2); var template = showSeconds ? this._$timeTemplate : this._$shortTimeTemplate; template[0] = hour; template[2] = minutes.stringify(); if (showSeconds) template[4] = seconds.stringify(); if (!show24) template[5] = (pm ? this.PMIndicator : this.AMIndicator); else template[5] = null; return template.join(isc.emptyString); }, //> @classMethod Time.parseInput() // Converts a time-string such as <code>1:00pm</code> to a date object with the appropriate // time set. Accepts most formats of time string. // @param string (string) time string to convert to a date // @visibility external //< // EXTREMELY forgiving of formatting, can accept the following: // 11:34:45 AM => 11:34:45 // 1:3:5 AM => 01:30:50 // 1:3p => 13:30:00 // 11 34 am => 11:34:00 // 11-34 => 11:34:00 // 113445 => 11:34:45 // 13445 => 01:34:45 // 1134 => 11:34:00 // 134 => 01:34:00 // 11 => 11:00:00 // 1p => 13:00:00 // 9 => 09:00:00 // Note: technically being passed "1:00" is ambiguous - could be AM or PM. // We always interpret as 24 hour clock (so <12 = AM) unless am/pm is actually passed in. // @param [validTime] (boolean) If passed, and the string passed in doesn't convert to // a valid time, return null rather than a date with time set to 00:00:00 parseInput : function (string, validTime) { var hours = 0, minutes = 0, seconds = 0, // We don't currently extract milliseconds from a time-string. Instead we zero them // out for consistency across times created by this method. milliseconds = 0; if (isc.isA.Date(string)) { hours = string.getUTCHours(); hours -= isc.Time.UTCHoursOffset; minutes = string.getUTCMinutes(); seconds = string.getUTCSeconds(); milliseconds = string.getMilliseconds(); } else if (string) { // iterate through the time expressions, trying to find a match for (var i = 0; i < isc.Time._timeExpressions.length; i++) { var match = isc.Time._timeExpressions[i].exec(string); if (match) break; } if (match) { // get the hours, minutes and seconds from the match // NOTE: this results in 24:00 going to 23:00 rather than 23:59... var hours = Math.min(parseInt(match[1]|0, 10),23), minutes = Math.min(parseInt(match[2]|0, 10),59), seconds = Math.min(parseInt(match[3]|0, 10),59), ampm = match[4]; ; if (ampm) { if (!this._pmStrings) this._pmStrings = {p:true, P:true, pm:true, PM:true, Pm:true}; if (this._pmStrings[ampm]) { if (hours < 12) hours += 12; } else if (hours == 12) hours = 0; } } else if (validTime) return null; } else if (validTime) return null; var date = new Date(); hours += isc.Time.UTCHoursOffset; // Catch the case where the UTC hours offset pushes the time past midnight or below zero while (hours >= 24) hours -= 24; while (hours < 0) hours += 24; date.setUTCHours(hours); date.setUTCMinutes(minutes); date.setUTCSeconds(seconds); date.setMilliseconds(seconds); return date; }, //> @classMethod Time.createDate() // Creates a date object with the time set to the hours, minutes and seconds passed in. // (Time is in UTC time, adjusted by +link{Time.UTCHoursOffset} if appropriate). // @param [hours] (number) Hours for the date (defaults to zero) // @param [minutes] (number) Minutes for the date (defaults to zero) // @param [seconds] (number) Seconds for the date (defaults to zero) // @visibility external //< // Support millseconds granularity too, though probably never used createDate : function (hours, minutes, seconds, milliseconds) { var date = new Date(); if (hours == null) hours = 0; else hours += isc.Time.UTCHoursOffset; // Catch the case where the UTC hours offset pushes the time past midnight or below zero // Note this logic means we differ from the behavior of parseInput - we would roll // times greater than 24 around the clock. while (hours >= 24) hours -= 24; while (hours < 0) hours += 24; date.setUTCHours(hours); if (minutes == null) minutes = 0; date.setUTCMinutes(minutes); if (seconds == null) seconds = 0; date.setUTCSeconds(seconds); if (milliseconds == null) milliseconds = 0; date.setMilliseconds(milliseconds); return date; }, //> @classMethod Time.setShortDisplayFormat() // Sets the default format for strings returned by +link{Time.toShortTime()}. // @param formatter (timeFormatter|function) Optional custom formatter to use. Will accept // a function (which will be passed a pointer to the date to perform the conversion), or // a string designating a standard formatter // @visibility external //< setShortDisplayFormat : function (format) { this.shortDisplayFormat = format; }, //> @classMethod Time.setNormalDisplayFormat() // Sets the default format for strings returned by +link{Time.toTime()}. // @param formatter (timeFormatter|function) Optional custom formatter to use. Will accept // a function (which will be passed a pointer to the date to perform the conversion), or // a string designating a standard formatter // @visibility external //< setNormalDisplayFormat : function (format) { this.displayFormat = format; }, //> @classMethod Time.compareTimes() // Compares the times of 2 dates, or strings. If a string is passed as one of the // parameters it should be in a format that converts to a valid time such as <code>"1:30pm"</code>, // <code>"13:30"</code>, or <code>"1:30:45pm"</code> // @param time1 (Date|string) First time to compare // @param time2 (Date|string) Second time to compare // @return (boolean) True if the times match, false if not // @visibility external //< compareTimes : function (time1, time2) { // If this method becomes time-critical we could speed this up by avoiding the // date conversion and having parseInput return just an array of H,M,S if (isc.isA.String(time1)) time1 = isc.Time.parseInput(time1); if (isc.isA.String(time2)) time2 = isc.Time.parseInput(time2); if (time1 == null && time2 == null) return true; // If we get non-dates at this point just return false - we don't want to be // comparing other types if (!isc.isA.Date(time1) || !isc.isA.Date(time2)) return false; return ((time1.getUTCHours() == time2.getUTCHours()) && (time1.getUTCMinutes() == time2.getUTCMinutes()) && (time1.getUTCSeconds() == time2.getUTCSeconds())); } });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -