获取24小时格式的时间,并使用Javascript AngularJS将其保存在任意变量中

Get time in 24 hrs format and hold it in any variable using Javascript AngularJS

本文关键字:AngularJS 保存 存在 变量 任意 Javascript 格式 24小时 时间 获取      更新时间:2023-09-26

我正在Javascript中创建一个时隙。请不要使用jQuery。如果你把第一次和最后一次,那么它会制作一个间隔列表,在12个数字之后,它会变成上午到下午。假设,如果你的开始时间是11,结束时间是19,那么它将产生一个像这样的插槽,

    11AM-12PM
    12PM-1PM
    1PM-2PM
    2pm-3pm
    3pm-4pm
    4pm-5pm
    5pm-6pm
    6pm-7pm

但是,问题是我把它作为一个简单的整数并给出for循环。我想保留它的24小时格式,比如

    1PM-2PM

它应该把这个值作为任何变量发送给服务器

    13:00:00 - 14:00:00

这是我的JS代码。

getTimeValues: function() {
                        console.log('here i am');
                        var timeValues = this.timeValues;
                        var minTime = 11; //vary this to change the first 
                        var maxTime = 19; //vary this to chage the
                        var string;
                        for (var i = minTime; i < maxTime; i++) {
                                if(i<12) {
                                        lowersuffix = 'AM';
                                        startRange = i;
                                        }
                                        else if(i===12){
                                                lowersuffix = 'PM';
                                                startRange = i;
                                        }
                                        else {
                                                lowersuffix = 'PM';
                                                startRange = i - 12;
                                        }
                                if((i+1)<12) {
                                        uppersuffix = 'AM';
                                        endRange = (i+1);
                                        }
                                        else if((i+1)===12){
                                                uppersuffix = 'PM';
                                                endRange = (i+1);
                                        }
                                        else {
                                                uppersuffix = 'PM';
                                                endRange = (i+1) - 12;
                                        }
                                string = startRange + lowersuffix + '-' + endRange + uppersuffix;
                                console.log(string);
                                timeValues.push(string);
                        };
                },

你在找这个吗?

 for (var i = minTime; i < maxTime; i++) {
      string = i + ':00:00 - ' + (i+1) + ':00:00';
      console.log(string);
      timeValues.push(string);
 };

似乎你是在一个简单的函数转换时间在am/pm格式为24小时格式。下面的代码应该可以完成这项工作,希望这些注释是足够的。如果没有,请询问。

/*  @param {string} time - time string in hh:mm:ss am/pm format
**                       - missing time parts are treated as 0
**  @returns {string} time in 24hr format: hh:mm:ss
**/
function to24hrTime(time) {
    // Get the digits from the string
    var b = time.match(/'d+/g);
    // Test whether it's am or pm, default to am
    var pm = /pm$/i.test(time);
    var h, m, s;
    // If no digits were found, return undefined
    if (!b) return;
    // Otherwise, convert the hours part to 24hr and two digits
    h = ('0' + ((pm? 12:0) + (b[0]%12))).slice(-2);
    // Convert minutes part to two digits or 00 if missing
    m = ('0' + (b[1] || 0)).slice(-2);
    // Convert seconds part to two digits or 00 if missing
    s = ('0' + (b[2] || 0)).slice(-2);
    // Return formatted string
    return h + ':' + m + ':' + s;
}
document.write(
  to24hrTime('3:15pm') + '<br>' +
  to24hrTime('3:15am') + '<br>' +
  to24hrTime('11am') + '<br>' +
  to24hrTime('11pm') + '<br>' +
  to24hrTime('12AM') + '<br>' +
  to24hrTime('12PM') + '<br>' +
  to24hrTime('11 pm') + '<br>' +
  to24hrTime('12 AM') + '<br>' +
  to24hrTime('12 PM')
);