by thangalin · 2y ago · 0 comments
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
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"It'll also convert "0:45" into "00:00AM" (and "0:23" into "11:00PM").
No, I wouldn't. Leaving out the leading zero is pretty common where I'm from.
// Preserve 2400h time by changing leading zeros to 24.
time = time.replace( /^0*[0:]/, '24' );That converts "0800" into "00:00AM".
time = time.replace( /^0*(0:)+/, '24' );