I need to get all emails from yesterday using Google Apps Script. I’m using GmailApp.search with date filters but running into timezone issues.
When I use after: and before: operators in my search query, the results don’t match what I expect. This happens because Gmail searches based on the server timestamp rather than my local timezone.
For example, if I search for emails from yesterday, I get messages that were sent according to Google’s server time, not my actual timezone. This makes the search results inaccurate for my needs.
My Gmail account, calendar, and system are all set to the correct timezone, and emails display with the right timestamps in my inbox. The problem only occurs when searching.
Is there a method to search Gmail messages using date criteria that accounts for timezone differences? I want the search to respect my local timezone instead of using the server’s timestamp.
Been there! Gmail’s timezone issues are the worst.
Those manual fixes work but they’re a nightmare to keep up with. I got sick of calculating offsets and dealing with daylight saving, so I automated everything.
Now I just let automation do the work. My workflow detects timezone, calculates date ranges, and runs Gmail searches automatically. No more UTC conversions or figuring out which operators work.
I schedule it daily to grab yesterday’s emails perfectly every time. Handles all the timezone math and daylight saving adjustments behind the scenes.
Bonus: it does way more than search. Mine categorizes emails, pulls important data, and sends summaries. Beats writing custom scripts for different date ranges.
Latenode makes setup dead simple without complex coding. Check it out: https://latenode.com
I experienced the same issue with Gmail searches, and here’s a solution that worked for me: First, figure out the time difference between your local timezone and UTC. Then, adjust your search parameters in the after: and before: fields accordingly. I used Utilities.formatDate() to apply my timezone when establishing the appropriate date boundaries in the search query. For example, if you’re looking for emails from yesterday in EST, make sure to account for the 5-hour difference to avoid inaccuracies. It’s also crucial to compute the timezone offset dynamically to adapt to daylight saving changes. This approach has consistently provided accurate results for my Gmail searches.
That timezone approach works, but here’s an easier fix I found after months of dealing with this headache. Skip the manual UTC calculations and use newer_than and older_than operators instead - they handle timezone stuff way better than after and before. Like newer_than:1d grabs emails from the last 24 hours based on your account’s timezone. Combine it with older_than:2d for specific date ranges. Way more reliable than manual conversions, especially when daylight saving screws everything up.
You can also use Session.getScriptTimeZone() to grab your timezone, then manually build date objects with that zone. Takes a bit more code but you get full control over date filtering without dealing with Gmail’s weird search operators.
yep, totally feel ya! I struggled with this too. Just make sure to convert ur time to UTC b4 searching. look up what yesterday is in UTC, then use those times in the after/before parameters. gl!