最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date Convert Military Hours - Stack Overflow

programmeradmin2浏览0评论

I have a date string formatted as:

20250330230300-0400

I am trying to get that string into the format of:

3/30/2025 11:03

What I was originally doing was something like this:

var formattedDateTime = dateString.substring(4,6) + '/' 
                      + dateString.substring(6,8) + '/' 
                      + dateString.substring(0,4) + ' ' 
                      + dateString.substring(8,10) + ':' 
                      + dateString.substring(10,12)
                      ;
    
//Remove leading 0s from day and month
formattedDateTime = formattedDateTime .replace(/0(\d)\/0?(\d)/g, '$1/$2');

What my code does not account for is the 23 military time being 11:03 instead of 23:02.
Can someone point me to either how to do this or a more efficient way of doing this?

I have a date string formatted as:

20250330230300-0400

I am trying to get that string into the format of:

3/30/2025 11:03

What I was originally doing was something like this:

var formattedDateTime = dateString.substring(4,6) + '/' 
                      + dateString.substring(6,8) + '/' 
                      + dateString.substring(0,4) + ' ' 
                      + dateString.substring(8,10) + ':' 
                      + dateString.substring(10,12)
                      ;
    
//Remove leading 0s from day and month
formattedDateTime = formattedDateTime .replace(/0(\d)\/0?(\d)/g, '$1/$2');

What my code does not account for is the 23 military time being 11:03 instead of 23:02.
Can someone point me to either how to do this or a more efficient way of doing this?

Share Improve this question edited Apr 1 at 12:05 Mister Jojo 22.5k6 gold badges25 silver badges44 bronze badges asked Mar 31 at 17:13 Johnny TJohnny T 193 bronze badges 9
  • 7 How will you know 11:03 is in the evening and not in the morning? – trincot Commented Mar 31 at 17:19
  • 2 Rather than using string replacement, I'd look into using Date and Time Formatting. – mykaf Commented Mar 31 at 17:27
  • what is -0400 values for ? a time zone offset ? – Mister Jojo Commented Mar 31 at 18:09
  • Why is ther no AM or PM in your result ? – Mister Jojo Commented Mar 31 at 18:10
  • 2 Why post a question here, only to ignore those who answer you? – Mister Jojo Commented 2 days ago
 |  Show 4 more comments

3 Answers 3

Reset to default -1

You can do that this way :

const f_DCMH = Md =>    // Date Convert Military Hours
  {
  let [_,Y,M,D,h,m] = Md.match(/(....)(..)(..)(..)(..)/);
  return `${+M}/${+D}/${Y} ${h==12?h:('0'+(h%12)).slice(-2)}:${m}`;
  }
// return `${+M}/${+D}/${Y} ${h==12?h:h%12}:${m}` ==> 1403 -> 2:03
  
console.log( f_DCMH('20250330230300-0400') ); // 3/30/2025 11:03
console.log( f_DCMH('20250330120300-0400') ); // 3/30/2025 12:03
console.log( f_DCMH('20250330140300-0400') ); // 3/30/2025 02:03

for info...

console.log(
  '20250330230300-0400'
  .replace(/(....)(..)(..)(..)(..)(.{7})/,'$2/$3/$1 $4:$5')
  );
  
// -> 03/30/2025 23:03

also:
see : World Time Zone Map

const 
  dtM_2_UTC = dtM => // date time military to UTC  conversion
    dtM.replace(/(....)(..)(..)(..)(..)(..)(...)(..)/,'$1-$2-$3T$4:$5:$6$7:$8')
    //            1     2   3   4   5   6   7    8       
, intl_UTC_D = new Intl.DateTimeFormat(
    'en-US', { year: 'numeric', month:  'numeric', day:       'numeric'
             , hour: '2-digit', minute: '2-digit', hourCycle: 'h12'
             , timeZone: 'Etc/GMT+4' }) // GMT+4 === UTC D (TimeTone Delta) 
  ;
let
  dte_UTC = dtM_2_UTC('20250330230300-0400')
, jsDate  = new Date( dte_UTC )
  ;

console.log('dte_UTC   -->', dte_UTC ); 
console.log('us, gmt+4 -->', intl_UTC_D.format(jsDate)); 

/*--- console = 

dte_UTC   --> 2025-03-30T23:03:00-04:00
us, gmt+4 --> 3/30/2025, 11:03 PM
----*/

Store the value in a variable, and use the modulus operator (%):

function convert(dateString) {
  let hour = +dateString.substring(8,10) % 12;
  if (hour == 0) hour = 12;
  hour = hour.toString().padStart(2, '0');

  const formattedDateTime = dateString.substring(4,6) + '/' + dateString.substring(6,8) + '/' + dateString.substring(0,4) + ' ' + hour + ':' +  dateString.substring(10,12);

  //Remove leading 0s from day and month
  return formattedDateTime.replace(/0(\d)\/0?(\d)/g, '$1/$2');
}

console.log('20250330230300-0400 ->', convert('20250330230300-0400'));
console.log('20250330210300-0400 ->', convert('20250330210300-0400'));
console.log('20250330000300-0400 ->', convert('20250330000300-0400'));
console.log('20250330030300-0400 ->', convert('20250330030300-0400'));

It should be: YYYYMMDDHHMMSS-OFFSET, based on UTC, so it should be read 2025-03-30, 23H03 , UTC-4.

Thus it should yield 7:03PM in that local time.

let m = "20250330230300-0400"

let date = new Date(m.substr(0,4) + "-" + m.substr(4,2) + "-" + m.substr(0,8).substr(-2))
date.setUTCHours(m.split("-")[0].substr(-6,2))
date.setUTCMinutes(m.split("-")[0].substr(-4,2))

// APPLY UTC SHIFT
date.setUTCHours(date.getUTCHours() - m.split("-")[1].substr(0,2))

console.log(
  date
)

console.log(
  date.toLocaleString("en-US")
)

It should be fine with date overlapping.

Chances are that using Date.UTC() would be simpler to use:

https://developer.mozilla./en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC

发布评论

评论列表(0)

  1. 暂无评论