I want a list of all yesterday's emails from gmail. I am trying to process it using google apps script, by writing a query on my inbox, and then using GmailApp.search. The after: and before: search query in gmail returns results that are not expected, since the query searches on the basis of the SMTP server time that the mail is sent from (which is google's server). Hence, being in a different time zone, the search yields inappropriate results to me. Is there a way to search gmail using a time criteria, so that I can accommodate for the time zone difference?
Please note that the local time zone, calendar, gmail etc. is correctly configured for my timezone, so the emails that I see in my inbox are correctly timed. Only the search is creating an issue.
Using keywords To find emails from after a specific date, use After:YYYY/MM/DD. So for example, if you're trying to find emails from before or after September 1st, 2021, type either Before:2021/09/01 or After:2021/09/01 into the search bar and hit Enter.
To search a specific range of dates, use your desired start and finish date range. For example, type “received:>=02/23/2018 AND received:<=02/25/2018. ” This sequence will retrieve all email received during the time period you specified.
Locate the email counter located on the right-hand side of the mailbox > Click on it and a small dropdown with 2 options will open, Newwest and Oldest. Click on the “Oldest” option to see sort your Gmail chronologically starting from your 50 oldest emails.
To search Google by date using this feature, submit your query. Hit the Tools button under the search bar. You should see two dropdowns appear, labeled Recent and Sort by Relevance. Both of these search by date tools can help you narrow things down.
Figured out a way after some trial and error, that it is indeed possible to search gmail emails by time. Notice that the Date()
returned in google apps script is according to your timezone.
The code below will return all previous day's emails in inbox, assuming new Date()
is giving the date and time according to your timezone. Division by 1000 is done because getTime()
returns milliseconds, while the newer / older search query expects seconds.
var month = new Date().getMonth();
var date = new Date().getDate();
var year = new Date().getFullYear();
var time1 = new Date(year, month, date, 0, 0, 0).getTime();
var time2 = time1 - 86400000;
var query = "newer:" + time2/1000 + " older:" + time1/1000 + " in:inbox";
var conversations = GmailApp.search(query);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With