Format time

K

ken

Mostly so I can find this again when I need it:

function formatTime(t) {
var myTime = t.value;
var badFormat = "Bad format";

// Ensure we have a value
if (myTime == "") {
return(true);
}

var regexSplit = /\s+/;
var regexTime = /:/;
var regexMili = /^([0-1][0-9]|2[0-3]):[0-5][0-9]$/; // 00:00 to
23:59
var regexHour = /^(0?[1-9]|1[0-2])$/;
var regexMin = /^[0-5][0-9]$/;
var regexAm = /^[aA]/;
var regexPm = /^[pP]/;

// split value into atoms
var atoms = t.value.split(regexSplit);

// If there is only one atom, are we using military time?
if ( (atoms.length) == 1) {
var timeAtom = atoms[0];
if (! regexMili.test(timeAtom) ) {
t.value = badFormat;
t.focus();
t.select();
return;
}
// Split time into hours, minutes
timeAtoms = timeAtom.split(regexTime);
var hours = parseInt(timeAtoms[0]);
var mins = timeAtoms[1];
var amPm = "am";
if ( hours > 11) {
if ( hours > 12) {
hours = hours - 12;
}
amPm = "pm";
} else if (hours == "0") {
hours = 12;
}
t.value = hours + ":" + mins + " " + amPm;
return(true);
}
var timeAtom = atoms[0];
var amPmAtom = atoms[1];

// Split time into hours, minutes
timeAtoms = timeAtom.split(regexTime);
var hours = timeAtoms[0];
var mins = timeAtoms[1];

// Check hours
if (! regexHour.test(hours)) {
t.value = badFormat;
t.focus();
t.select();
return;
}

// Check minutes
if (! regexMin.test(mins)) {
t.value = badFormat;
t.focus();
t.select();
return;
}

// Check am
if (regexAm.test(amPmAtom) ) {
var amPm = "am";
} else if (regexPm.test(amPmAtom) ) {
var amPm = "pm";
} else {
t.value = badFormat;
t.focus();
t.select();
return;
}

t.value = hours + ":" + mins + " " + amPm;
return(true);
}

See http://kenandrenee.com/timeReformatJs.html for working example.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Wed, 21 Mar 2007 11:46:04, (e-mail address removed) posted:
Mostly so I can find this again when I need it:

This is not the place for that. Use your own Web site.
function formatTime(t) {

Overlong and underdocumented. What there is seems to be written in a
peculiarly ambiguous transatlantic dialect - military time can refer
either to the normal 24-h clock or to the use of letters designating
time zone or offset from Greenwich.

There's no need to validate a time (or date) numerically with RegExps
when new Date() already can do that succinctly. But use a RegExp if it
is also necessary to validate a less liberal character pattern.


Also badly designed. It is most inconvenient that, for test, the same
control is used for input and for output.

The code gives output in FFF. It throws away seconds. It claims to
accept standard time, yet it rejects "11:11:11" which is in accordance
with the applicable ISO (and, IIRC, US FIPS) standard. It rejects
24:00, which is allowed by ISO 8601.



The following code, lightly tested, should take any reasonable exact-
seconds time string and return it in standard form - except that *IF*
locally Date.toString() uses am/pm then the result will be 12-h without
am/pm :

Query - does Date.toString() ever give time
other than as hh:mm:ss on the 24-hour clock?

S = "11:33 " // test
T = new Date("1/1/1 "+S).toString().match(/(\d\d:\d\d:\d\d)/)[1]

If Date.toString() can use the 12-hour clock, replacing the .match()[1]
with .replace(/.*(\d\d:\d\d:\d\d)( ?)\s*([ap]\.?m\.?)?/, "$1$2$3") may
serve.

With that approach, omitting seconds is trivial.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top