15

Is it possible to search all Gmail messages within a range more granular than the day?

For instance, could I search for all chat messages from 3pm to 5pm on October 4th? How would that look?

Looking at the Gmail advanced search reference I noticed that they mention the specific times, but do not specify the format, most likely it is not supported, but I thought I would check just in case I was simply entering searches in the wrong format...

Example: after:2004/04/16 before:2004/04/18

Meaning: Messages sent between April 16, 2004 and April 18, 2004.*

*More precisely: Messages sent after 12:00 AM (or 00:00) April 16, 2004 and before April 18, 2004.

2 Answers 2

13

It is possible to specify time range with accuracy up to a second, because the search operators after:, before:, newer:, older: accept Unix timestamps. Using a tool such as Epoch Converter, you can find out that

  • 2014-10-04 at 15:00 (using GMT for example) has timestamp 1412434800
  • Add two hours (7200 seconds), or use the same tool again: 1412442000

Searching for after:1412434800 before:1412442000 will return the messages within this 2-hour range.

Since converting to Unix time looks like a chore, I made a bookmarklet that makes this substitution in place.

For example, after typing

after:2015/07/26 10:00 before:2015/07/27 11:30

in the search box (which would not work as is), invoke the bookmarklet and the string will be replaced by

after:1437919200 before:1438011000

(The bookmarklet interprets the given timestamps in your local time.)

The page I linked has both the source and a draggable link with the bookmarklet. For completeness, the source code is also posted below.

inp = document.querySelectorAll('input');
for (i in inp) {
  if (/(after|before|newer|older):/.test(inp[i].value)) {
    str = inp[i].value;
    times = str.match(/\d[\d\/: ]*\d/g);
    for (j in times) {
      if (Date.parse(times[j])) {
        str = str.replace(times[j], Date.parse(times[j])/1000);
      }
    }
    inp[i].value = str;
  } 
}
6
  • It seems that after:UNIXTIMESTAMP is not working properly...
    – Kunok
    Nov 28, 2016 at 15:06
  • @Kunok seems to work to me. could you please elaborate?
    – nakhli
    Nov 20, 2017 at 15:23
  • 1
    @nakhli Ugh..this is old. But as far as I remember I think I gave it too many int chars as input. I think I had to remove last 3 digits or something like that. Somewhat as this answer above mentions, note example in answer, it has 10 chars, while Date.now() would return 13 chars. Something like that. I fixed it and script works ever since then, it's still running 0-24 last 12 months, and probably gonna run for a very long time. Thanks. :P
    – Kunok
    Nov 20, 2017 at 15:26
  • 13 characters because it returns milliseconds Sep 24, 2019 at 11:23
  • This works on the gmail query search bar as well and is documented in the gmail API docs. You can search by the second developers.google.com/gmail/api/guides/filtering Sep 15, 2020 at 6:41
9

The specific times are mentioned in the documentation to show that the "after" operator is inclusive of the date given, while the "before" operator is exclusive of the date. There is no way to use the search box on a more specific time range.

If you are using Google Apps Scripts, you can make your search using the best dates you have. Then you can filter the resulting threads based on Time and other criteria. Example:

var threads = GmailApp.search("after:2014/09/01 before:2014/09/02",0,100);
var timeFrom = new Date("2014/09/01 10:00:00").getTime();
var timeTo = new Date("2014/09/01 13:00:00").getTime();

for (var i = 0; i < threads.length; i++){
  var timeThreadStart = threads[i].getFirstMessageDate().getTime();

  if(timeThreadStart > timeFrom && timeThreadStart < timeTo){
    //do something
  }
}
3
  • NOTE: "after" operator is inclusive of the date given, while the "before" operator is exclusive of the date!!
    – user34125
    Nov 2, 2016 at 11:47
  • Hi Pyrodogg, can I join with use label ? GmailApp.search
    – Pathic
    Jan 29, 2019 at 9:46
  • If I understand your question correctly, yes you can use labels also in the search criteria. Ex. "after:2019/01/01 before:2019/02/01 label:purchases". If I didn't understand your question, please let me know.
    – Pyrodogg
    Feb 3, 2019 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.