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?
3 Answers
Reset to default -1You 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
-0400
values for ? a time zone offset ? – Mister Jojo Commented Mar 31 at 18:09AM
orPM
in your result ? – Mister Jojo Commented Mar 31 at 18:10