Skip to content Skip to sidebar Skip to footer

Get Time In 24 Hrs Format And Hold It In Any Variable Using Javascript Angularjs

I am creating a time slot in Javascript. Please no jQuery. If you put the first time and last time, then it will make a lists of interval and after 12 number, it will change to am

Solution 1:

Are you looking for this?

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

Solution 2:

It seems you're after a simple function to convert time in am/pm format to 24hr format. The following should do the job, hopefully the comments are sufficient. If not, ask.

/*  @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
**/functionto24hrTime(time) {

    // Get the digits from the stringvar b = time.match(/\d+/g);

    // Test whether it's am or pm, default to amvar pm = /pm$/i.test(time);
    var h, m, s;

    // If no digits were found, return undefinedif (!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 stringreturn 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')
);

Post a Comment for "Get Time In 24 Hrs Format And Hold It In Any Variable Using Javascript Angularjs"