Есть ли плавный способ добавить функцию в другой код? - PullRequest
0 голосов
/ 18 мая 2019

Я устанавливаю код для таймера, и мне нужно иметь currentTime в правильном часовом поясе.Поэтому я имею к разным codes.Один code с правильным currentTime, а другой code, который только делает local время.Я пытаюсь добавить code, который имеет правильный timezone, в другой code, но я не могу заставить его работать.

Вот code с timezone:

var $ = window.jQuery;

var setTime = function(){
  var date = moment().tz("Europe/Paris");
  
  var day = date.day();
      
  console.log('date', date);
  console.log('day', day);
  
  var time = date.format("h:mm A");
  
  console.log(time, time);
  
  $('#current-time').html("Current time: " + time + " ");
};

setTime();

А вот code по местному времени.

var current = new Date();
var day = current.getDay();
var currentTime = (currentTime) = (current.getHours() * 60) + current.getMinutes();
var remainTime = 0;
var closeTime = (openTime[day].close *60);
var openTime = (openTime[day].open *60);

1 Ответ

0 голосов
/ 18 мая 2019

Вы всегда можете использовать функцию Date() и рассчитать смещение времени.

Например:

function calcTime(city, offset) {
    // create Date object for current location
    var d = new Date();

    // convert to msec
    // subtract local time zone offset
    // get UTC time in msec
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time for the city "+ city +" is "+ nd.toLocaleString();
}

alert(calcTime('Paris', '+2'));

Получено с здесь

...