Atlaslog
Atlaslog / Examples / Cesium / Time Convert
Cesium · Runnable Example

Time Convert

Runnable Cesium example: Time Convert. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All Cesium examples
/demos/cesium-time-convert.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-time-convert.html
<!--
  示例:儒略日与常用日期时间互转
  要点:JulianDate.fromIso8601 / toIso8601 / addSeconds / secondsDifference 的配合
  依赖:CesiumJS 1.121 · jsDelivr CDN,输入用本地时间控件
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>儒略日换算</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
  html,body{height:100%;margin:0}
  body{background:#0e1116;color:#e6e8ec;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  #c{height:100%}
  .hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.93);
       border:1px solid #2a313b;border-radius:6px;padding:10px 12px;font-size:12px;
       color:#a2a9b6;width:290px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:7px}
  .kv{display:flex;justify-content:space-between;gap:8px;line-height:1.9}
  .kv span:first-child{color:#7f8896}
  .kv span:last-child{font-family:ui-monospace,Consolas,monospace;color:#8fb7e0;font-size:11px}
  input[type=datetime-local]{width:100%;font:inherit;font-size:12px;background:#1d232b;
       color:#cfd6e2;border:1px solid #343b46;border-radius:4px;padding:3px 6px;margin-top:6px}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
  <div><b>儒略日换算</b></div>
  <div class="row">
    <input id="dt" type="datetime-local" value="2026-10-28T12:00">
  </div>
  <div class="row">
    <div class="kv"><span>ISO 8601</span><span id="iso">—</span></div>
    <div class="kv"><span>dayNumber</span><span id="day">—</span></div>
    <div class="kv"><span>secondsOfDay</span><span id="sec">—</span></div>
    <div class="kv"><span>当天合计秒</span><span id="total">—</span></div>
    <div class="kv"><span>加 1 小时后</span><span id="plus">—</span></div>
    <div class="kv"><span>与基准差</span><span id="diff">—</span></div>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
  'use strict';

  var viewer = new Cesium.Viewer('c', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: false, timeline: false, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });

  var scene = viewer.scene;
  scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  scene.skyBox.show = false;
  scene.sun.show = false;
  scene.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;

  /* 基准时刻:2026-10-28T00:00:00Z */
  var BASE = Cesium.JulianDate.fromIso8601('2026-10-28T00:00:00Z');

  var TOWER = Cesium.Cartesian3.fromDegrees(116.389, 39.906, 0);

  viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(116.389, 39.906, 90),
    cylinder: {
      length: 180, topRadius: 22, bottomRadius: 40,
      material: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.85),
      outline: true,
      outlineColor: Cesium.Color.fromCssColorString('#cfe4ff')
    }
  });

  var stamp = viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(116.389, 39.906, 210),
    label: {
      text: '—',
      font: '600 13px "Microsoft YaHei", sans-serif',
      fillColor: Cesium.Color.fromCssColorString('#ffd18a'),
      pixelOffset: new Cesium.Cartesian2(0, -8),
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    }
  });

  function refresh() {
    var value = document.getElementById('dt').value;
    if (!value) return;

    /* 本地时间控件给的是无时区字符串,按本地时区构造 Date 再转儒略日 */
    var date = new Date(value);
    var jd = Cesium.JulianDate.fromDate(date);
    var iso = Cesium.JulianDate.toIso8601(jd, 0);
    var later = Cesium.JulianDate.addSeconds(jd, 3600, new Cesium.JulianDate());
    var diff = Cesium.JulianDate.secondsDifference(jd, BASE);

    document.getElementById('iso').textContent = iso;
    document.getElementById('day').textContent = jd.dayNumber;
    document.getElementById('sec').textContent = Math.round(jd.secondsOfDay);
    document.getElementById('total').textContent =
      Math.round(jd.dayNumber * 86400 + jd.secondsOfDay);
    document.getElementById('plus').textContent = Cesium.JulianDate.toIso8601(later, 0);
    document.getElementById('diff').textContent = Math.round(diff) + ' 秒';

    stamp.label.text = iso.slice(0, 19).replace('T', ' ');

    /* 点也跟着时间偏移在东西方向来回摆动 */
    var swing = Math.sin(diff / 3600) * 0.004;
    stamp.position = Cesium.Cartesian3.fromDegrees(116.389 + swing, 39.906, 210);

    viewer.camera.setView({
      destination: Cesium.Cartesian3.fromDegrees(116.389, 39.898, 780),
      orientation: { heading: 0, pitch: Cesium.Math.toRadians(-12), roll: 0 }
    });
  }

  document.getElementById('dt').addEventListener('input', refresh);
  refresh();
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More Cesium examples

Cesium · EXAMPLETime Dynamic Point Cloud Cesium · EXAMPLETime Dynamic Wheels Cesium · EXAMPLEUnderground Color