function convertToGMT(timeWithAMPM, Timezone) { console.log('timeWithAMPM >>>----------------------------->',timeWithAMPM); const inputTime = new Date(); if (!timeWithAMPM || typeof timeWithAMPM !== 'string') { return null; } const [time, ampm] = timeWithAMPM.split(' '); const [hours, minutes] = time.split(':'); if (ampm.toLowerCase() === 'pm') { inputTime.setHours(hours + 12); } else { inputTime.setHours(hours); } inputTime.setMinutes(minutes); const offsetInMinutes = Timezone === 'IST' ? 330 : 0; const gmtTime = new Date(inputTime.getTime() - offsetInMinutes * 60000); return gmtTime && gmtTime.toUTCString(); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter