2014-12-24 97 views
0

我有一個輸入字段:格式字符串像HH時間格式:毫米

<input id="szReminderTime" type="text" value="" maxlength="5" 
    onblur="format_reminder_time(this.value);" 
    name="askForQuoteAry[szReminderTime]" /> 

Time字段的格式是hh:mm在24小時制,例如7:3011:4516:1019:11,22:43

如果操作類型的期間(11.45),逗號(11,45),一個空格(11 45),破折號(11-45),或全無(1145945),然後每個這些應被認爲具有同樣的意思。然後,一旦操作員離開現場,值應該以冒號顯示,即11:459:45

爲了達到這個目的,我使用了以下JavaScript函數,它對我來說工作正常,但任何人都可以優化我的代碼,因爲我的代碼對我來說看起來不太好?

+0

題外話(見codereview.stackexchange.com),你甚至沒有張貼代碼 – Alnitak

+0

「下面的代碼」在哪裏? – RobG

+0

此問題不是[*如何格式化javascript日期*]的重複(http://stackoverflow.com/questions/3552461/how-to-format-javascript-date)。沒有涉及日期或日期對象,操作系統詢問如何重新格式化代表時間的字符串。 – RobG

回答

1

如果這只是一個日期格式化位置,我會建議您使用plain javascipt來進行格式化。

如果不是http://momentjs.com/是大多數日期格式化問題的解決方案。

moment("20111031", "YYYYMMDD").fromNow(); // 3 years ago 

http://momentjs.com/docs/#/displaying/

你比如這純粹是"hh:mm"如您在說明有它。

+0

認爲'moment.js'對於OP想要實現的內容來說有點矯枉過正。 :-) – 0x2D9A3

+0

這將要求OP確定要處理的字符串格式(例如hh:mm,h.mm,h,mm,hhmm),然後對每個字符串單獨調用moment.js。按照OP所要求的功能只有6行代碼,這可能比各種moment.js調用所需要的要少(並且根本不需要庫)。 – RobG

+0

正如我所說,如果這是一個計時器,但通過它的外觀,他使用了很多日期的東西。 因此@roG解決方案是更好的挑選任何lib如果它是一個單一的。 –

0

將24小時時間轉換爲12小時時間的功能相當簡單,但您有一些特殊要求。考慮以下幾點:

// Convert string in 24 hour time to 12 hour hh:mm ap 
// Input can be 12:23, 945, 09,12, etc. 
function from24to12(s) { 
    var b = s.replace(/\D/g,''); 
    var h = b.substring(0, b.length - 2); 
    var m = b.substring(b.length - 2); 
    return (h%12 || 12) + ':' + m + ' ' + (h>11? 'PM':'AM'); 
} 

console.log(from24to12('23:15')); // 11:15 PM 
console.log(from24to12('015')); // 12:15 AM 
console.log(from24to12('1.15')); // 1:15 AM 

這裏假設你不希望在小時和前導零的運營商將在兩位數總是關鍵的幾分鐘,例如9.03,而不是9.3。爲了支持後者,需要3行代碼。

下支持分隔的任何字符,也說9.3上午9:03:

// Convert string in 24 hour time to 12 hour hh:mm ap 
// Input can be 12:23, 945, 09,12, etc. 
// Sseparator can be any non-digit. If no separator, assume [h]hmm 
function from24to12(s) { 
    function z(n){return (n<10?'0':'')+n} 
    var h, m, b, re = /\D/; 

    // If there's a separator, split on it 
    // First part is h, second is m 
    if (re.test(s)) { 
    b = s.split(re); 
    h = b[0]; 
    m = z(+b[1]); 

    // Otherwise, last two chars are mm, first one or two are h 
    } else { 
    h = s.substring(0, s.length - 2); 
    m = s.substring(s.length - 2); 
    } 
    return (h%12 || 12) + ':' + m + ' ' + (h>11? 'PM':'AM'); 
} 

console.log(from24to12('23:15')); // 11:15 AM 
console.log(from24to12('005')); // 12:05 AM 
console.log(from24to12('1.15')); // 1:15 AM 
console.log(from24to12('17.5')); // 5:05 PM 
0

你可以在Javascript中做到這一點很容易。您可以在此基礎上擴展以符合您的要求。 請看看我的腳本:

Date.prototype.dateToday = function (syntax) { 
    var dateToday = null; 

    if (syntax === 'dd-mm-yyyy') { 
     dateToday = (
     ((this.getDate() < 10) ? '0' + this.getDate() : this.getDate()) + '-' + ((this.getMonth() + 1 < 10) ? '0' + this.getMonth() : this.getMonth()) + '-' + this.getFullYear()); 

    } else if (syntax === 'dd/mm/yyyy') { 
     dateToday = (
     ((this.getDate() < 10) ? '0' + this.getDate() : this.getDate()) + '/' + ((this.getMonth() + 1 < 10) ? '0' + this.getMonth() : this.getMonth()) + '/' + this.getFullYear()); 

    } else if (syntax === 'dd-mm-yy') { 
     var year = this.getFullYear().toString(); 

     dateToday = (
     ((this.getDate() < 10) ? '0' + this.getDate() : this.getDate()) + '-' + ((this.getMonth() + 1 < 10) ? '0' + this.getMonth() : this.getMonth()) + '-' + year.substr(2, 4)); 

    } else if (syntax === 'dd/mm/yy') { 
     var year = this.getFullYear().toString(); 

     dateToday = (
     ((this.getDate() < 10) ? '0' + this.getDate() : this.getDate()) + '/' + ((this.getMonth() + 1 < 10) ? '0' + this.getMonth() : this.getMonth()) + '/' + year.substring(2,4)); 
    } 

    return dateToday; 

}; 

Date.prototype.timeNow = function (syntax) { 
    var timeNow = null; 

    if (syntax === 'hh:mm:ss') { 
     timeNow = (
     ((this.getHours() < 10) ? '0' + this.getHours() : this.getHours()) + ':' + ((this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes()) + ':' + ((this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds())); 
    } else if (syntax === 'hh:mm') { 
     timeNow = (
     ((this.getHours() < 10) ? '0' + this.getHours() : this.getHours()) + ':' + ((this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes()) + ':' + ((this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds())); 

    } else { 
     timeNow = (
     ((this.getHours() < 10) ? '0' + this.getHours() : this.getHours()) + ':' + ((this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes()) + ':' + ((this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds()) + '.' + ((this.getMilliseconds() < 10) ? '0' + this.getMilliseconds() : this.getMilliseconds())); 

    } 

    return timeNow; 
} 

Date.prototype.hourNow = function() { 
    var hours = ((this.getHours() < 10) ? '0' + this.getHours() : this.getHours()); 
    return hours; 
} 

Date.prototype.minuteNow = function() { 
    var minutes = ((this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes()); 
    return minutes 
}; 

Date.prototype.secondNow = function() { 
    var seconds = ((this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds()); 
    return seconds; 
}; 

Date.prototype.milisecondsNow = function() { 
    var milliseconds = ((this.getMilliseconds() < 10) ? '0' + this.getMilliseconds() : this.getMilliseconds()); 
    return milliseconds; 
}; 

或者看看我的Git這個助手:datehelper.js

相關問題