How to convert Unix time according to browser time javascript

I want to show the time according to the user location of. site link , In the website there is a section which shows the show timing, I want the time display according to the user utc.

(function() {  

      var weekday = new Array(7);
      weekday[0] =  "Sunday";
      weekday[1] = "Monday";
      weekday[2] = "Tuesday";
      weekday[3] = "Wednesday";
      weekday[4] = "Thursday";
      weekday[5] = "Friday";
      weekday[6] = "Saturday";


      function formatDate(date) {
          var hours = date.getHours();
          var minutes = date.getMinutes();
          var ampm = hours >= 12 ? 'PM' : 'AM';
          hours = hours % 12;
          hours = hours ? hours : 12; // the hour '0' should be '12'
          minutes = minutes < 10 ? '0'+minutes : minutes;
          var strTime = hours + ':' + minutes + ' ' + ampm;
          return weekday[date.getDay()] +  " " + strTime;
        }

      $('.stream-box-sources-list-item').each( function(){
          var $$ = $(this),
          $elmnt = $$.find('.stream-source-stream-date');
          $time_scedule = new Date( $elmnt.attr('rel-time')  * 1000 );

          $elmnt.text(formatDate($time_scedule));
      } );

  })();`

1 answer

  • answered 2017-10-23 22:48 meza

    Honestly, you can do this by hand by storing the show time in UTC time, then converting the user's time to UTC and then comparing the 2.

    However, I'd recommend using momentJS for all date related things, as they do a hands down much better job at it than any of us trying to make it work by hand. There's so many problems around timezones, I recommend not falling down that rabbit hole.