JDE/Julian Time: How to format julian time stamp number

radi8 picture radi8 · Apr 12, 2013 · Viewed 8.2k times · Source

In my system, time stamps are returned using the old IBM julian format.

For example:
12 o'clock 0 minutes and 1 seconds AM (1 sec after midnight) is returned 01.
12 o'clock 22 minutes and 15 seconds AM is returned 2215.
1 o'clock 22 minutes and 15 seconds AM is returned 12215.
7 o'clock 45 minutes and 1 seconds AM is returned 74501.
7 o'clock 22 minutes and 15 seconds PM is returned 192215.

I need a regex expression to put these into the format of:
12 o'clock 0 minutes and 1 seconds AM (1 sec after midnight): 00:00.01
12 o'clock 22 minutes and 15 seconds AM: 00:22.15
1 o'clock 22 minutes and 15 seconds AM: 01:22.15
7 o'clock 45 minutes and 1 seconds AM: 7:45.01
7 o'clock 22 minutes and 15 seconds PM: 19:22.15

Any help is appreciated.

SOLUTION Thanks to MikeM, here is the solution:

//var time = '01';
//var time = '2215';
//var time = '12215';
//var time = '74501';
var time = '192215';

time = time.replace( /^(?:(?:(\d)?(\d))?(\d\d))?(\d\d)$/,
  function ( all, hr1, hr2, min, sec ) {
    return (hr1 || '0') + (hr2 || '0') + ':' + (min || '00') + '.' + sec;
  }
);

Answer

MikeM picture MikeM · Apr 12, 2013

The following works with your examples, though I haven't tested it beyond that

//var time = '01';
//var time = '2215';
//var time = '12215';
//var time = '74501';
var time = '192215';

time = time.replace( /^(?:(?:(\d)?(\d))?(\d\d))?(\d\d)$/,
  function ( all, hr1, hr2, min, sec ) {
    return (hr1 || '0') + (hr2 || '0') + ':' + (min || '00') + '.' + sec;
  }
);

Although it gives 07:45.01 not 7:45.01, so as to be in keeping with 01:22.15.