0
votes

I am having a few problems with dates and times in Google Sheets. What I have is:

Spreadsheet 1 has a number of sheets one of which is called Sessions. On that sheet there are a number of columns which show all sorts of data about individual training sessions. I then have a second spreadsheet which is published to a Google site which the users can see. It only has one sheet which is populated by a QUERY + IMPORTRANGE to get the information from the Sessions tab of the first spreadsheet. The reason I do this is I don't want the users to be able to see all the different sheets in the first spreadsheet nor do I want them to see all the different columns in there either. There is one more reason as well. I want the query to filter the sessions to only show ones that are happening in the future, not ones that have already passed.

The query I have in the second sheet to pull the data is:

=query(importrange("IDofMainSpreadsheet","Sessions!A:I");"select Col1,Col2,Col3,Col6")

Now that works fine. What I want to add is something like:

=query(importrange("IDofMainSpreadsheer","Sessions!A:I");"select Col1,Col2,Col3,Col6 where Col2 >= Now()")

I have tried many different solutions but can't seem to find one that works.

Ok.... Have made some developments. It's not pretty but if I add another sheet and create a cell with the following:

 =YEAR(now()) & "-" & DEC2OCT(MONTH(now()), 2) & "-" & DAY(now()) &" " & HOUR(now()) &":"& MINUTE(now()) &":"& SECOND(now())

then I can use this in the query:

 =query(importrange("IDofSpreadsheet","Sessions!A:I");"select Col1,Col2,Col3,Col6  where Col2  >= datetime '"&Sheet3!B3&"'  ")

Would be great if someone could help me do this in a single line. Have tried the toDate(Now()) option as suggested below but this does not return any results whereas the above does.

Any thoughts?

2
this is not really a Google apps script question unless you want to create a new custom function or use a script to do what you want ? Don't you think ?Serge insas
Agree, have changes tags sorryMikeE

2 Answers

1
votes

In the QUERY select clause, now() will produce a date-time, which I presume you are comparing with dates. Try:

=QUERY(ImportRange("IDofMainSpreadsheer","Sessions!A:I");"select Col1,Col2,Col3,Col6 where Col2 >= toDate(now())")

https://developers.google.com/chart/interactive/docs/querylanguage#scalar_functions

edit

The fact that your workaround is working suggests that you actually have datetimes in column 2. Below I have a suggestion for why your original solution might not be working, but first a more compact version of your workaround:

=QUERY(ImportRange("IDofSpreadsheet","Sessions!A:I");"select Col1,Col2,Col3,Col6 where Col2 >= datetime '"&TEXT(NOW();"yyyy-MM-dd HH:mm:ss")&"'")

The only thing I can think of is that now() in a QUERY select clause and NOW() invoked in a spreadsheet formula appear to operate in different time zones, and may not necessarily return the same result. For example, right now in my Eastern Australia time zone, the former returns a datetime on 25/07/2012 (presumable Mountain View time), and the latter (correctly) returns a datetime on 26/07/2012. So it is probably good practice (at this stage) to use the spreadsheet NOW() as you have done in your workaround.

0
votes

Or just need to change the time zone on your google spreadsheet.