2014-12-24 54 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

相关问题