2014-12-24 7 views
0

는 I는 입력 필드가 :형식 문자열 : mm

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

Time 필드의 형식은, 예를 들어 24 시간에 hh:mm7:30, 11:45, 16:10, 19:11, 22:43.

기간 (11.45), 쉼표 (11,45), 공간 (11 45), 대시 (11-45) 또는 아무것도 (1145 또는 945)을, 이들의 각각이있는 것으로 간주되어야 오퍼레이터 유형이면 같은 뜻. 그런 다음 작업자가 필드를 벗어나면 값이 콜론 (예 : 11:45 또는 9:45)으로 표시되어야합니다.

이 코드를 작성하려면 다음 JavaScript 함수를 사용하십시오. 내 코드는 내게 잘 보이지 않으므로 누구나 내 코드를 최적화 할 수 있습니까?

+0

(codereview.stackexchange.com 참조)하고 심지어 코드를 게시하지 않았다 – Alnitak

+0

"다음 코드"는 어디에 있습니까? – RobG

+0

이 질문은 [* javascript date * 서식 지정 방법] (http://stackoverflow.com/questions/3552461/how-to-format-javascript-date)과 중복되지 않습니다. 날짜 또는 날짜 개체가 관련된, OP는 시간을 나타내는 문자열을 다시 포맷하는 방법을 묻습니다. – RobG

답변

1

위치를 지정하는 날짜가 단 한 번이라면 plain javascipt을 사용하여 형식을 지정하는 것이 좋습니다.

그렇지 않은 경우 http://momentjs.com/은 대부분의 날짜 형식 문제에 대한 해결책입니다. 당신이 설명에있는 귀하의 예를 들어

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

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

그냥 "hh:mm"이 될 것입니다.

+0

'moment.js '는 OP가 얻고 자하는 것에 대한 과잉 공격이라고 생각하십시오. :-) – 0x2D9A3

+0

그러면 OP에서 처리 할 문자열 형식을 결정해야합니다 (예 : hh : mm, h.mm, h, mm, hhmm). 그런 다음 각각에 대해 moment.js를 별도로 호출해야합니다. OP가 필요로하는 기능은 단지 6 줄의 코드로, 다양한 moment.js 호출에 필요한 것보다 적을 수 있습니다 (라이브러리를 전혀 필요로하지 않습니다). – RobG

+0

내가 평범한 자바 스크립트를 언급 한 것처럼 이것이 하나의 타이머이지만 더 많은 데이트 물건을 사용하고있다. 그래서 @robG 솔루션은 onetimer 인 경우 모든 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 

이것은 당신이 시간에 그 앞에 0을 원하지 않는 것으로 가정 운영자 것이다 분 동안 두 자리에 항상 키, 예를 들어, 9.3이 아니라 9.03. 후자를 지원하려면 3 줄의 코드가 더 필요합니다.

다음은 분리에 대한 모든 문자를 지원하며 오전 9시 3분 9.3 말 : 당신은 아주 쉽게 자바 스크립트에서이 작업을 수행 할 수

// 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

. 요구 사항에 맞게이 작업을 확장 할 수 있습니다.

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; 
}; 

을 또는이 도우미에 대한 내 힘내 봐 걸릴 : 내 스크립트에서 봐 주시기 바랍니다 주제 오프 datehelper.js

관련 문제