How Can I Retrieve the Date of the Earliest Gmail Thread with Apps Script?

I’m working on a Google Apps Script that helps me identify the very first email thread in my Gmail inbox and then extract the date of its initial message. My goal is to accurately determine when the earliest conversation was started. I would appreciate guidance on the correct method to obtain this date using the Gmail service in Apps Script. Below is an updated example of my approach. I’m interested in any suggestions for improving the script or if there’s an alternate, more efficient method to achieve this.

var inboxThreads = GmailApp.getInboxThreads(0, 1);
var earliestThread = inboxThreads[0];
var initialMsg = earliestThread.getMessages()[0];
Logger.log(initialMsg.getDate());

The script you posted is a good starting point for retrieving the first message date of a thread, but it has its pitfalls. In my experience, getInboxThreads retrieves threads based on the current device’s sorting rules, which might not always yield the actual earliest thread if the inbox is subject to dynamic ordering or label changes. Using GmailApp.search with a query based on date range or specific labels gives you more control over your search criteria. I found that this approach allows me to verify the results more reliably in a large mailbox.