something like that ?
function date2unix(oldDate, bTime)
{
let [d,m,y] = oldDate.split('-').map(Number)
, [h,mn,ap] = bTime.split(/:| /).map(s=>Number(s)||(/pm/i.test(s)?12:0))
;
return new Date( y, --m, d, (h % 12) +ap, mn, 0 ).getTime()
}
sample usage:
function date2unix(oldDate, bTime)
{
let [d,m,y] = oldDate.split('-').map(Number)
, [h,mn,ap] = bTime.split(/:| /).map(s=>Number(s)||(/pm/i.test(s)?12:0))
;
return new Date( y, --m, d, (h % 12) +ap, mn, 0 ).getTime()
}
let oldDate = '10-03-2021'
, b = '7:45 AM'
;
let unixDate = date2unix(oldDate, b)
console.log('oldDate =', oldDate , `\n`, ' b =', b, `\n ----->` )
console.log('unixDate =', unixDate )
/* -- verif -- */
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
let verifDate = new Date(unixDate)
console.log('\nverif:\n', verifDate.toLocaleString(), `\n`, verifDate.toLocaleString('en-US',options) )
.as-console-wrapper { max-height: 100% !important; top: 0; }
For info, there is a trap with the AM PM notation :
_ 12am is Midnight ( 00:xx )
_ 12pm is Noon ( 12:xx )
see https://en.wikipedia.org/wiki/12-hour_clock
// on AM -> ap = 0
// on PM -> ap = 12
// ... so :
if (h===12) h = 0 // then h + ap will return the correct hour value
// or simply (without if)
h %= 12 // in the code :: (h % 12) + ap :: will return the correct hour value