5 ms·
On a related note, here's an algorithm for parsing user input into a sanitized time format: String.prototype.toTime = function () { var time = this;
by thangalin 2y ago
On a related note, here's an algorithm for parsing user input into a sanitized time format:
String.prototype.toTime = function () {
var time = this;
var post_meridiem = false;
var ante_meridiem = false;
var hours = 0;
var minutes = 0;
if( time != null ) {
post_meridiem = time.match( /p/i ) !== null;
ante_meridiem = time.match( /a/i ) !== null;
// Preserve 2400h time by changing leading zeros to 24.
time = time.replace( /^00/, '24' );
// Strip the string down to digits and convert to a number.
time = parseInt( time.replace( /\D/g, '' ) );
}
else {
time = 0;
}
if( time > 0 && time < 24 ) {
// 1 through 23 become hours, no minutes.
hours = time;
}
else if( time >= 100 && time <= 2359 ) {
// 100 through 2359 become hours and two-digit minutes.
hours = ~~(time / 100);
minutes = time % 100;
}
else if( time >= 2400 ) {
// After 2400, it's midnight again.
minutes = (time % 100);
post_meridiem = false;
}
if( hours == 12 && ante_meridiem === false ) {
post_meridiem = true;
}
if( hours > 12 ) {
post_meridiem = true;
hours -= 12;
}
if( minutes > 59 ) {
minutes = 59;
}
var result =
(""+hours).padStart( 2, "0" ) + ":" + (""+minutes).padStart( 2, "0" ) +
(post_meridiem ? "PM" : "AM");
return result;
};
I posted it a while back to the following thread:
https://stackoverflow.com/a/49185071/59087 https://stackoverflow.com/a/49185071/59087
One of my minor bug bears is why we humans have to enter time into input fields in such rigid formats when there's a straightforward algorithm to make such inputs far more natural.
- Denvercoder9 2y agoYour function converts "0:23" into "11:00 PM".
- thaumasiotes 2y agoIf you were a human, you would have written "00:23", not "0:23". It'll also convert "0:45" into "00:00AM" (and "0:23" into "11:00PM").
- Denvercoder9 2y ago> If you were a human, you would have written "00:23", not "0:23". No, I wouldn't. Leaving out the leading zero is pretty common where I'm from.
- namtab00 2y agoYou come from a weird place.
- Denvercoder9 2y agoDon't worry, we say the same about the US, where 12pm comes before 11pm.
- thaumasiotes 2y agoDoes that mean that in your country, the day is considered to start at 1:00 AM rather than midnight? That choice is very strange when combined with the choice to call midnight "0:00" rather than "12:00 PM".
- Denvercoder9 2y agoNo, we just don't use AM/PM. In written language, the 24 hour format is used exclusively. When the 12 hour format is used in spoken language, we either leave it out entirely when clear from context, or use "in the morning" vs "in the evening". So noon is "12 in the morning", which is an hour after "11 in the morning"; and midnight is "12 in the evening", which is an hour after "11 in the evening".
- thaumasiotes 2y agoThat's not really compatible with your comment about the US. By your own description, you do exactly the same thing that the US does,† but it's only strange when the US does it? † That is, you follow 11:00 PM with 12:00 AM, and you don't actually refer to "AM" or "PM". Both of those are normal US practice. You are different in the use of 24-hour formats in writing, but that has no bearing on the question of whether the hour following 11 PM is 12 AM; you just assured me that that's true in both countries.
- thangalin 2y agoGood catch. Easy fix: // Preserve 2400h time by changing leading zeros to 24. time = time.replace( /^0*[0:]/, '24' );
- thaumasiotes 2y ago> Easy fix: That converts "0800" into "00:00AM".
- thangalin 2y agoTrue enough, the regex was overzealous. This looks better: time = time.replace( /^0*(0:)+/, '24' );
- archargelod 2y ago> One of my minor bug bears is why we humans have to enter time into input fields in such rigid formats when there's a straightforward algorithm to make such inputs far more natural. GNU date supports very human-oriented date input, some examples: $ date --date "today" $ date --date "next sunday" $ date --date "now + 3 hours" $ date --date "3 months" # same as "now + 3 months" $ date --date "5 december 2020" $ date --date "oct 19 2007" $ date --date "08:00" $ date --date "8:00" $ date --date "0800" $ date --date "0800 UTC"