Search code examples
javascriptcesiumjs

how to set 10 mins before the particular time in Cesium


I was asked to post the particular time on the clock view model. The particular time refers to 1504108564(Wednesday, August 30, 2017 11:56:04 PM GMT+08:00).. I want to set 10 mins before this timing. i want to put pTime in the clock view model.But it does not work... My code is found below...

var pTime = 1504108564;
var tenMinuteOffset = Cesium.JulianDate.addSeconds(pTime, -600, new Cesium.JulianDate());

var clock = new Cesium.Clock({
    startTime : tenMinuteOffset,
    currentTime : tenMinuteOffset,
    clockRange : Cesium.ClockRange.LOOP_STOP,
    clockStep : Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER
});

var viewer = new Cesium.Viewer('cesiumContainer', {
    infoBox : false,
    selectionIndicator : false,
    shadows : true,
    clockViewModel: new Cesium.ClockViewModel(clock)
});

My question is i want to use this timestamp in the clock... and i want to set 10 mins before the timestamp Do i need to convert the pTime to readable time? How to do it?


Solution

  • The first argument to addSeconds should be a reference to a Cesium.JulianDate, not an integer number.

    Give this a try:

    var pTime = Cesium.JulianDate.fromIso8601('2017-08-30T11:56:04+08');
    var tenMinuteOffset = Cesium.JulianDate.addSeconds(pTime, -600, new Cesium.JulianDate());
    
    var clock = new Cesium.Clock({
        startTime : tenMinuteOffset,
        currentTime : tenMinuteOffset,
        clockRange : Cesium.ClockRange.LOOP_STOP,
        clockStep : Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER
    });
    
    var viewer = new Cesium.Viewer('cesiumContainer', {
        infoBox : false,
        selectionIndicator : false,
        shadows : true,
        clockViewModel: new Cesium.ClockViewModel(clock)
    });
    

    EDIT: If you really want to start from the timestamp, you can do that, but beware the timestamp doesn't have a timezone baked into it. So, using the timestamp will have different results based on the timezone setting of the client's web browser, not the server. The first few lines of the above code would look like this:

    var pTime = 1504108564;
    var julian = Cesium.JulianDate.fromDate(new Date(pTime * 1000));
    var tenMinuteOffset = Cesium.JulianDate.addSeconds(julian, -600, new Cesium.JulianDate());