You can parse the string to a Date then use toISOString, but beware of the built–in parser, see Why does Date.parse give incorrect results?. Note that this will also parse the string as local, then produce a UTC timestamp.
Alternatively you can parse the string manually and just reformat the parts, it it's already UTC, or parse it as local and produce a UTC timestamp, e.g.
// No Date object
// Reformat 3/17/2021, 2:23:24 PM as ISO 8601 assuming input is UTC
function reformatDate0(d) {
let [M, D, Y, h, m, s, ap] = d.trim().split(/\W+/);
let z = n => ('0'+n).slice(-2);
return `${Y}-${z(M)}-${z(D)}T${z((h%12) + (/am/i.test(ap)?0:12))}:${m}:${s}.000Z`;
}
console.log('0: ' + reformatDate0('3/17/2021, 2:23:24 PM'));
// Using a Date object, values as UTC
// Reformat 3/17/2021, 2:23:24 PM as ISO 8601 assuming input is UTC
function reformatDate1(d) {
let [M, D, Y, h, m, s, ap] = d.trim().split(/\W+/);
return new Date(
Date.UTC(Y, M-1, D, (h%12) + (/am/i.test(ap)?0:12), m, s)
).toISOString();
}
console.log('1: ' + reformatDate1('3/17/2021, 2:23:24 PM'));
// Using a Date object, values as local
// Reformat 3/17/2021, 2:23:24 PM as ISO 8601 assuming input is local
function reformatDate2(d) {
let [M, D, Y, h, m, s, ap] = d.trim().split(/\W+/);
return new Date(Y, M-1, D, (h%12) + (/am/i.test(ap)?0:12), m, s).toISOString();
}
console.log('2: ' + reformatDate2('3/17/2021, 2:23:24 PM'));
Note that the first two treat the input at UTC, last treats it as local so the time is shifted by the host timezone offset for the input date and time.