Justin
Justin

Reputation: 11

convert string to UTC time using jquery

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.

Upvotes: 0

Views: 213

Answers (2)

arizafar
arizafar

Reputation: 3132

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'))

Upvotes: 1

Roger the Shrubber
Roger the Shrubber

Reputation: 73

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,

Upvotes: 0

Related Questions