Lookup Current Day and Time with QUERY or VLOOKUP - google-sheets

I currently pull in some appointment time slots based on the current time now() and extract the timeslots from the adjacent worksheet.
I now want to add in the current day of the week as an argument as well. Would I need to do something more advanced like =query or how could I extend this to the current =vlookup?
Here are what I got so far with QUERY and VLOOKUP:
=query(TimeSlots!C2:F25, select 2,3,4,5 where Col1 = Live!A2 & where Col2 = Live!B2)
=vlookup(timevalue(B2), {iferror(value(regexextract(TimeSlots!B2:B, "(.+)\s-"))),TimeSlots!C2:F}, {2, 3, 4, 5}, 1))
https://docs.google.com/spreadsheets/d/1OOdLI-1j3d10LsfGdNPS4tOusx4AnRZvXzdgmrLvrSA/edit?usp=sharing
If you want to have a play and Make a Copy, here links.

I write this in good faith with your success at heart; things are a bit more difficult for you than they need be because you are breaking a couple of the unwritten rules of spreadsheet normalization. Most prominently, if you wish to use a cell in a formula elsewhere, the cell should be in a Sheets-native format. Try this:
Select TimeSlots column B
From the menus, choose Insert → Column Right
Rename B and C to, say… "Time Slot Begin" and "Time Slot End", respectively
Edit the data to recreate your ranges in this new split arrangement (included below if you want to copy-paste)
Armed with uniform, natively-calculable data sources, you can now use a formula to get the row(s) where the weekdays match and the current time is greater than the slot's start time and the current time is less than the slot's end time.
For example:
=FILTER( TimeSlots!A2:G29,
A$2 = TimeSlots!A2:A29,
MOD(B$2,1) > TimeSlots!B2:B29,
MOD(B$2,1) < TimeSlots!C2:C29
)
Line by line, in English, it: narrows down the list of Time Slots to rows where
this sheet's day matches the TimeSlot's day, and
this sheet's time is after (greater than) the Time Slot's beginning, and
this sheet's time is before (less than) the Time Slot's ending.
Note that NOW() outputs a datetime, of which you are showing just the time using "cosmetic" formatting. To only look at the actual time component of the B2 date-time, I'm dividing by 1 day and keeping the remainder time.
Here are the aforementioned columns of time starts and ends, if you want to paste them into place. Column B:
Time Slot Begin
07:00:00
09:00:00
16:00:00
20:00:00
07:00:00
09:00:00
16:00:00
20:00:00
07:00:00
09:00:00
16:00:00
20:00:00
07:00:00
09:00:00
16:00:00
20:00:00
07:00:00
09:00:00
16:00:00
20:00:00
07:00:00
09:00:00
16:00:00
20:00:00
07:00:00
09:00:00
16:00:00
20:00:00
and Column C:
Time Slot End
08:59:00
15:59:00
19:59:00
06:59:00
08:59:00
15:59:00
19:59:00
06:59:00
08:59:00
15:59:00
19:59:00
06:59:00
08:59:00
15:59:00
19:59:00
06:59:00
08:59:00
15:59:00
19:59:00
06:59:00
08:59:00
15:59:00
19:59:00
06:59:00
08:59:00
15:59:00
19:59:00
06:59:00

Related

Google Sheets formula to convert FROM ordinal dates TO standard dates

I am looking for a formula that will automatically convert a column of ordinal date/time values into standard date/time values.
July 1st 2022 6:48:49 pm
July 2nd 2022 10:03:35 am
July 3rd 2022 5:41:12 pm
July 4th 2022 2:44:13 pm
I need to get those reformatted to eliminate the st, nd, rd, and th portions.
Sheet
You can use
=INDEX(IF(A2:A7="",, REGEXREPLACE(A2:A7,"st|nd|rd|th","")*1))
You can then format as you like
(Do adjust the formula according to your ranges and locale)
Functions used:
INDEX
IF
REGEXREPLACE

How to convert a text string in Google Sheets to date format

I have a column with dates and time formatted like this in each cell:
Thursday, Jan 21, 2021 4:30 PM-5:00 PM
I want to split this across two columns so that the first column has "DD/MM/YY" and the second has the timeslot.
So it would go from being a cell with:
Thursday, Jan 21, 2021 4:30 PM-5:00 PM
to two cells:
21/01/21 4:30 PM-5:00 PM
What formula can I use in Google Sheets to achieve this?
Another suggestion (which assumes here that your raw data runs A2:A):
=ArrayFormula(IF(A2:A="",,SPLIT(REGEXREPLACE(A2:A,"^\w+, (.+\d) (\d.+$)","$1~$2"),"~")))
This will leave your dates in the first column as numeric raw dates rather than as text, so you'd be able to use them in calculations and comparisons later. Just select the first column of the results (i.e., those raw dates, showing as numbers in th 40000 range) and format the entire column (Format > Number) in the date format you prefer.
use:
=INDEX(IFNA(TEXT({REGEXEXTRACT(A1:A, ", (.+\d{4})")*1,
REGEXEXTRACT(A1:A, "\d{4} (.+)")}, {"dd/mm/yyyy", "#"})))

Select approriate cell based on current time

Have been scratching my head for the last hour now.
I have a timetable with time values in Column A and then activity on Column B
e.g.
COLUMN A | COLUMN B
0.3333333333 EAT
0.3715277778 SLEEP
0.4097222222 PRAY
0.4444444444 DO NOTHING
0.46875 .
0.5034722222 .
0.5416666667 .
0.5798611111
0.625
0.6666666667
What I want to do is excel to tell me what I should be doing now, and what I should be doing next.
I can get the current time like this: =MOD(NOW(),1)
I'm thinking I need to turn the value of =MOD(NOW(),1) into exactly one of the values in column A, then I can do a VLOOKUP to tell me what I need to be doing now/next (in Column B)
I've been playing with MATCH, CHOOSE, I have to use some nested AND to find out if the current time is before or after the times in Column A. But I just can't figure this one out.
Many thanks.
Well, you can do it with a few simple steps.
First, as I understand in column A you have start time, then in column B you need end time, in cell B2 put =A3.
So you can have something like:
start end to-do
08:00:00 08:55:00 eat
08:55:00 09:50:00 go
09:50:00 10:40:00 something
10:40:00 11:15:00 .
11:15:00 12:05:00 .
12:05:00 13:00:00
13:00:00 13:55:00
13:55:00 15:00:00
15:00:00 16:00:00
16:00:00
It's very easy from there.
Add one column on your left (for vlookup purpose). Let's say you have current time in cell G2. Then in your new column A in cell A2 put a formula:
=AND($G$2>=B2;$G$2<C2)
to check if your current time is in the time range you have in your start and end.
If your current time is 11:45 then you should have something like this:
start end to-do
FALSE 08:00:00 08:55:00 eat
FALSE 08:55:00 09:50:00 go
FALSE 09:50:00 10:40:00 something
FALSE 10:40:00 11:15:00 .
TRUE 11:15:00 12:05:00 .
FALSE 12:05:00 13:00:00
FALSE 13:00:00 13:55:00
FALSE 13:55:00 15:00:00
FALSE 15:00:00 16:00:00
FALSE 16:00:00
then all you need to do is vlookup where TURE is.
EDIT: to check what you should do next you need vlookup nested in vlookup. First instead using vlookup to find what value is in column to-do you need to vlookup value in the column end (12:05:00). Then this value can be set as value to look up in range columns start:to-do.

Year query returns no results

I have a Holiday model, with a holiday_date attribute of DateTime.
I added a new Holiday (New Years Day) with a date of 1/1/2019.
When I do in the console Holiday.last, I see this:
#<Holiday id: 50, name: "New Years Day", holiday_date: "2018-12-31 23:00:00", created_at: "2018-11-13 13:15:54", updated_at: "2018-11-13 13:15:54">
So it is saved in UTC time, a day earlier. When I then do Holiday.last.holiday_date I get this:
Tue, 01 Jan 2019 00:00:00 CET +01:00
Great, the date is converted to our CET date and time. But when I query for a year like this:
Holiday.where("extract(year from holiday_date) = '2019'")
It returns no results. So it seems that there is no conversion to CET time with this query. How can I make sure that the query returns the holiday I added?
You'll have to cast timezones twice:
Holiday.where(
"extract(year from holiday_date AT TIME ZONE 'UTC' AT TIME ZONE 'CET') = '2019'"
)
This will work, but it would be nice to use indices for your query, we'll just have to prepare it better:
year = Time.zone.parse("2019-01-01")
Holiday.where("holiday_date BETWEEN ? AND ?", year.beginning_of_year, year.end_of_year)
# SELECT "holidays".* FROM "holidays" WHERE (holiday_date BETWEEN '2018-12-31 23:00:00' AND '2019-12-31 22:59:59.999999')
I would really think whether you need datetimes for your holiday_date column, perhaps dates would be enough, so that you don't have to deal with timezones.
You can query by timezone like this
Holiday.where("extract(year from holiday_date AT TIME ZONE 'CET') = '2019'")

moment-timezone format doesn't return the expected result

Using the tz() function from moment-timezone as follow:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format()
//returns '2017-10-15T13:53:43+08:00'
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format('h:m A')
//I expect to return '9:53 PM' but it returns '1:53 PM'
Ultimately, I want to apply the fromNow() function to format the result. But when I apply it, it uses the initial timestamp and ignore the timezone applied.
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').fromNow()
//I expect to return '1 min ago' when actual time is 13:54 UTC (21:54 in HK) but it returns '8 hours ago'
What am I doing wrong here?
When you do:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong');
You're creating a date/time that corresponds to October 15th 2017, at 1:53 PM in Hong Kong - which, in turn, corresponds to 2017-10-15T05:53:43Z (5:53 AM in UTC).
When you call the format() function:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format();
It returns:
2017-10-15T13:53:43+08:00
The +08:00 part is just the UTC offset - it just tells that Hong Kong is 8 hours ahead UTC. But 2017-10-15T13:53:43+08:00 (1:53 PM in Hong Kong) is exactly the same instant as 2017-10-15T05:53:43Z (5:53 AM in UTC). That's why fromNow(), when the current time is 13:54 UTC, returns 8 hours.
If you want the date/time that corresponds to 1:53 PM in UTC, you should use the utc() function:
// October 15th 2017, 1:53 PM in UTC
moment.utc('2017-10-15 13:53:43');
Now, when the current time is 13:54 UTC, fromNow() will return 1 minute (because the date/time represents 1:53 PM in UTC).
To convert this to Hong Kong timezone, just use the tz() function:
// convert 1:53 PM UTC to Hong Kong timezone (9:53 PM)
moment.utc('2017-10-15 13:53:43').tz('Asia/Hong_Kong').format('h:m A');
This will convert 1:53 PM UTC to Hong Kong timezone (resulting in 9:53 PM):

Resources