0

I am trying to convert military time from a string with regex... what I have so far is

var a = "[18:37:56]";
var b = a.replace(/(\[|\])/g,'');
var c = b.match(/^[0-9][0-9]/);
var d = Number(c[0]) > 12 ? Number(c[0]) - 12 : Number(c[0]);
b.replace(/^[0-9][0-9]/,d);

The first replace is to get rid of the square brackets. Then we match the first two numbers, then after this we will see if that number is greater than 12, if so subtract 12 if not leave it alone. Then replace those numbers with the corresponding number.

Problem is for one, what if the for c there is no second number, meaning it's only at 12:01am or 9:59am in standard time. I'm seeing a lot of flaws that can come up with this, does anyone have any better solutions than what I've done?

8
  • 1
    Use modulo, 18 % 12 === 6 Commented Feb 18, 2014 at 23:58
  • Can you explain modulo is that with Vanilla JS? and what is the % for in terms of here. Commented Feb 18, 2014 at 23:58
  • 1
    See here en.wikipedia.org/wiki/Modular_arithmetic Commented Feb 18, 2014 at 23:59
  • change var c = b.match(/^[0-9]{1,2}/); to handle the single number issue Commented Feb 19, 2014 at 0:00
  • 1
    The modulo operator gives you the remainder of a division. It's useful for many things, like figuring out if a number is odd or even, do something every nth iteration, etc... Commented Feb 19, 2014 at 0:05

2 Answers 2

3

One-liner:

alert("[18:37:56]".substr(1,8).replace(/^\d\d?/,function(m) {return (m%12)||12;}));

If you want to append am or pm:

"[18:37:56]".replace(/\[(\d\d?)(:\d\d:\d\d)\]/,function(_,h,ms) {return ((h%12)||12)+ms+(h>=12?"p":"a")+"m";});
Sign up to request clarification or add additional context in comments.

7 Comments

This looks nice and simple, though the issue arrises again with the substring as it won't always have 8 characters ;)
See edit - the second code does everything at once and allows for the single-digit case (although usually military time has a leading zero anyway)
Ok let me see how this chat works, I'm going to change my time on my computer (if that is what it's going by) and see if their is a leading zero) if not whatever ;)
I took it off. Realized just now that I clicked it when reading.
@NiettheDarkAbsol why is your regex looking through the minutes and seconds? Just curious mate
|
0

You should probably just use dates...

var time = "[18:37:56]";
var date = new Date("1 january 2014 " + time.substring(1, time.length - 1));

date.getHours() % 12 == 6
date.getMinutes() == 37
date.getSeconds() == 56

By the way, dates do need day/month/year, but since every day has the same number of hours, you can enter any date you want. It's the hours, minutes and seconds that matter.

5 Comments

This is for a chat functionality that is already implemented on our forum sites. We don't have access to the php so I do these little JS 'hacks'. I don't think this would be very useful in the long run.
This is javascript... Javascript has a Date type.
HAHA I know this, I'm saying the implementation of this isn't very plausible unless it's going off my time or PHP time frame.
I... don't see what php time has to do with this. You just take the time string "[18:37:56]" as you got it in your original post, and you extract the hours, minutes and seconds from it?
Oh ok I see what your doing. Your using the string as the input of the new date then modular it for the hours. ;) I thought you were using the gethours for the hours. That's a big Simpson D'oh :D lol my bad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.