Gewusst wie: laden Sie die Zeitzone-Daten in Moment-timezone.js
Ich bin kämpfen, um die Zeitzone-Daten geladen, die für die Verwendung durch moment-timezone.js. Unten ist mein code (der Kürze halber habe ich nur Hinzugefügt, 2 Zonen, aber wirklich brauchen, um viel mehr):
<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript"></script>
<script>
moment.tz.add([
"America/Chicago|CST CDT EST CWT CPT|60 50 50 50 50|01010101010101010101010101010101010102010101010103401010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261s0 1nX0 11B0 1nX0 1wp0 TX0 WN0 1qL0 1cN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 11B0 1Hz0 14p0 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x30 iw0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0",
"America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1nX0 11B0 1nX0 11B0 1qL0 1a10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x40 iv0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0"
]);
var userTimeZone = document.getElementById("dom-target-usertimezone");
var deviceTimeZone = document.getElementById("dom-target-devicetimezone");
console.debug("User timezone: " + userTimeZone.textContent);
console.debug("Device timezone: " + deviceTimeZone.textContent);
console.debug("Loaded tz names: " + moment.tz.names());
var deviceNow = moment.tz(moment().utc().valueOf(),deviceTimeZone.textContent);
console.debug("User: " + moment(deviceNow).format());
var time = moment(deviceNow).format('h:mmA');
console.debug("time = " + time);
</script>
Bekomme ich die folgende Ausgabe
User timezone:
America/Chicago
Device timezone:
America/New_York
Loaded tz names: America/Chicago,America/New_York
Moment Timezone has no data for
America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.
User: 2015-04-26T12:38:02+00:00
time = 12:38PM
So, wie Sie sehen können, ist der moment.tz.Namen() weist darauf hin habe ich New York geladen, doch wenn ich Versuch zum einstellen der UTC Zeitzone zu New York, habe ich gesagt, dass es keine Daten. Hilfe. Danke.
LÖSUNG:
<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.min.js" type="text/javascript"></script>
var deviceTimeZone = document.getElementById("dom-target-devicetimezone").textContent.trim();
var deviceNow = moment.tz(deviceTimeZone);
var time = deviceNow.format('h:mmA');
document.getElementById('clock').innerHTML = time;
InformationsquelleAutor SteveSTL | 2015-04-26
Du musst angemeldet sein, um einen Kommentar abzugeben.
Haben Sie geladen, moment-timezone zweimal, und die geladenen Daten zweimal. Es ist wohl immer verwirrt.
Beide der oben genannten Anweisung laden Sie die moment-timezone-code. Sie sollte nur einer von Ihnen.
Wenn Sie geladen werden, bestimmte Zonen mit
moment.tz.add
, dann sollten Sie dasmoment-timezone
Skript. Wenn Sie einschließen möchten alle Zonen, dann verwenden Sie eine dermoment-timezone-with-data
scripts.Nicht laden
moment-timezone-with-data
und verwenden Sie dannmoment.tz.add
, so dass nur eine Kopie der Zoneninformationen, die bereits in der Daten-Datei.Außerdem kann der code vereinfacht werden:
Ich bin mir nicht sicher, warum Sie wollten es formatiert, zweimal in zwei verschiedene Wege, aber dieser code ist äquivalent zu dem, was du geschrieben hast.
Moment Timezone has no data for America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/. logError @ moment-timezone-with-data-2010-2020.js:297 fn.tz @ moment-timezone-with-data-2010-2020.js:371 tz @ moment-timezone-with-data-2010-2020.js:315 startTime @ device.js:19 onload @ index.php?q=device&t=10:68
Was ist Zeit-Zeitzone erwarten, da die timezone? Ich übergeben Sie als string.
Behoben! Ich getrimmt Sie die Zeichenfolge, und jetzt ist es funktioniert. Danke!
wie trimmen die timezone-string oder, wie zu bauen timezone-string?
Wir verwenden bower für JS Abhängigkeiten. Gibt es eine Möglichkeit, pull-down "- moment-timezone-mit-Daten" mithilfe von bower?
InformationsquelleAutor Matt Johnson