Date query with the current date between two date_

2019-07-24 14:42发布

问题:

I have a fusion table with two date_time columns. The fist one is the start date (Startdatum) and in the other column is the end date (Einddatum).

I want to do a query with the current date, and only show the KML-lines on a map where the current date lies between the start and end date.

I tried to use the code below to create a string with a date format:

var time_date = new Date();
var day = time_date.getDate(); 
var month = time_date.getMonth()+1;
var year = time_date.getFullYear();
var date = (year+"."+month+"."+day);

To show the KML-lines on the map I tried to use the following code:

 layer = new google.maps.FusionTablesLayer({
  map: map,
  heatmap: { enabled: false },
  query: {
    select: "col2",
    from: "1mOMP1seJq4FdiNTugsfylZaJc8sKcSlfJKUuTJjv",
    where: "'Startdatum' <= date AND 'Einddatum' >= date"

  },
  options: {
    styleId: 2,
    templateId: 2
  }
});

Unfortunatly the map shows all the KMS-lines regardless what date is in one of the columns.

What am I doing wrong?

回答1:

  1. the where-clause is wrong, it has to be

    where: "Startdatum <= '"+date+"' AND Einddatum >= '"+date+"'"
    
  2. the date-format seems to be wrong. Although the used format yyyy.MM.dd is defined in the documentation, it doesn't work. The format yyyy-MM-dd currently works for me(but it's not defined in the documentation).

    var date = (year+"-"+month+"-"+day); 
    

    (in case that day and month be less than 10 they wouldn't match the pattern, but that doesn't seem to be an issue)

Beyond that: when you fix these 2 mentioned parts it currently works(for me), but I've tried it a couple of hours ago and got unstable results.