Search code examples
javascriptdatetimezonetimezone-offsettimestamp-with-timezone

Show exact time with GMT JavaScript


The given time is in a format of ISO_8601 E.g: "2019-04-05T13:30-05:00" E.g: "2005-08-09T18:31+03:30" should return an exact time in a readable format with the GMT which is mentioned E.g. Sat, Nov 30, 2019, 11:00 PM GMT+1

Input: "2019-04-05T13:30-05:00" Output: "FRI, APR 5, 2019, 1:30 PM GMT-5"

Input: "2005-08-09T18:31+03:30" Output: "TUE, AUG 9, 2005, 6:31 PM GMT+3"


Solution

  • I suggest you use Momentjs, here is a simple snippet how to do it with moment.

    function getDateString(inputDate) {
      let date = moment.parseZone(inputDate);
    
      return date.format('ddd, MMM D, YYYY, h:mm A zZ')
    }
    
    console.log(getDateString("2005-08-09T18:31+03:30"));
    console.log(getDateString("2019-04-05T13:30-05:00"));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>