Home>
The external JSON file is read by Google Maps API and the pin is displayed.
It narrows down the pins to be displayed using jQuery.grep when reading JSON.
In that case, I would like to compare the date and time described in the JSON file with the current date and time so that only the past is displayed, but the JSON time is the world standard time.
I want to convert this to Japan time and compare it with the current time, but I don't know how to write the conversion.
I would like to know if you know.

The source is as follows.

var map;
var markers = [];
var infowindow = new google.maps.InfoWindow ();
function initialize () {
geocoder = new google.maps.Geocoder ();
var center = new google.maps.LatLng (XXX, XXX);
map = new google.maps.Map (document.getElementById ('map'), {
zoom: 12,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
markMultiple ();
}
function markMultiple () {
$.getJSON ('JSON file', function (data) {
  data = $.grep (data, function (obj, i) {
    var now_time = new Date ();
    var now_y = now_time.getFullYear ();
    var now_m = ("0" + (now_time.getMonth () + 1)). slice (-2);
    var now_d = ("0" + now_time.getDate ()). slice (-2);
    var now_h = ("0" + now_time.getHours ()). slice (-2);
    var now_i = ("0" + now_time.getMinutes ()). slice (-2);
    var now_s = ("0" + now_time.getSeconds ()). slice (-2);
    var now_l = ("0" + now_time.getMilliseconds ()). slice (-3);
    var now = now_y + '/' + now_m + '/' + now_d + '' + now_h + ':' + now_i + ':' + now_s;
    return now>obj.date
  });
  $.each (data, function (i, obj) {
       var latLng = new google.maps.LatLng (obj.map_latitude, obj.map_longitude);
       var content = content of the balloon after clicking the pin;
       markMap (latLng, content);
     });
    var markerCluster = new MarkerClusterer (map, markers);
   });
}
function markMap (position, content) {
var marker = new google.maps.Marker ({
position: position,
icon: 'img/icon.png'
});
google.maps.event.addListener (marker, 'click', function () {
infowindow.setContent (content);
infowindow.open (map, marker);
});
markers.push (marker);
}
google.maps.event.addDomListener (window, 'load', initialize);

JSON is as follows.

[
  {
    "id": "0",
    "station": "Shibuya Station",
    "timer": "",
    "date": "2004-12-22T15: 00: 00.000Z",
    "map_latitude": XXX,
    "map_longitude": XXX,
  },
  {
    "id": "1",
    "station": "Harajuku Station",
    "timer": "on",
    "date": "2004-12-23T15: 00: 00.000Z",
    "map_latitude": XXX,
    "map_longitude": XXX,
  }
]
  • Answer # 1

    Change time zone

    In Japan,new Date ("Timestamp") It will change to the Japanese time zone without permission.

    new Date ("2004-12-22T15: 00: 00.000Z");// =>Thu Dec 23 2004 00:00:00 GMT + 0900 (Japan Standard Time )

    Compare dates

    Date.now ()-new Date ("2004-12-22T15: 00: 00.000Z");

  • Answer # 2

    This part Write only the source.

    return now_time.getTime ()>new Date (obj.date) .getTime ();

    Returns true if in the past.

    .getTime () is

      

    January 1, 1970 00:00:00 UTC A numeric value representing the elapsed time from the specified date and time in milliseconds.
      https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime#Return_value

    is returned, so you can compare with that number of seconds.


    As mentioned in the previous answer, the return value ofnew Date ("timestamp")is
    It can be adjusted to the time zone of the computer.

    So don't worry about the timestamp.

    If you are interested, please do as follows.

    var offset = +9
    return now_time.getTime ()>new Date (new Date (obj.date) .getTime () + (new Date (). getTimezoneOffset () + offset * 60) * 60000);