0

I am trying to convert string to time, the string i have are in this format, '8:3' and '16:45'.

I want to convert UTC time in jQuery.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
Justin
  • 11
  • 2

2 Answers2

1

You can write your function to create UTC date with the time string.

function toUTC(str) {
  let [h, m] = str.split(':');
  let date = new Date();
  date.setHours(h, m, 0)
  return date.toUTCString();
}

console.log(toUTC('8:3'))
console.log(toUTC('16:45'))
AZ_
  • 3,094
  • 1
  • 9
  • 19
0

You don't need jQuery for such operations. Just the simple Date object will do the trick. Say you want to convert time from a specific date.

let date = new Date('2020-04-01'); // leave the Date parameter blank if today
date.setHours(16); // must be 24 hours format
date.setMinutes(45);
let theUTCFormat = date.getUTCDate();

Cheers,