NEW YORK (AP) — Booking a flight is often confusing, annoying and frustrating. Prices fluctuate so frequently that most vacationers can’t tell if they are getting a good deal.

We check multiple websites, often several times a day. Then, a few days after we finally do book that non-refundable ticket, there’s a sale, confirming that we overpaid.

So when is the best time to book a flight? One travel site dug through the data and has an answer for us: 54 days in advance. Well, except there are plenty of caveats. We’ll get back to that number in a minute, but first a little bit about how the process works.

Airlines use sophisticated computer programs to analyze booking trends and constantly change prices to get the most money out of each flight. That’s why two passengers in the same row might have paid vastly different fares, depending on when they booked. Complicating matters is a bevy of fees added to help the airlines offset higher jet fuel prices.

That’s why booking at the right time is so much more important today. The average cost of a roundtrip domestic ticket – including baggage and reservation change fees – grew to

$378.62 from $351.48 in the last five years, when adjusted for inflation. That brings us to 54 days.

For a study published in February, booking site CheapAir.com looked at millions of trip combinations, searching as far as 320 days in advance to one day prior to departure and every possible day between.

That’s 1.3 billion airfares. The result: 54 days in advance was the best time, on average, to buy domestic tickets. This is not a hard-and-fast rule, however.

Airfares to popular vacation destinations tend to go up sooner. So flights to Phoenix, San Diego, Orange County, Calif., as well as Fort Lauderdale, West Palm Beach, Pensacola, and Orlando in Florida were actually cheapest 75 days in advance, according to CheapAir’s study. For Las Vegas, it was 81 days and for airports in Hawaii it was 87 days.

Confused yet? That’s why CheapAir tried to simplify things and come up with a more-general rule: The prime booking window is 29 to 104 days before departure.

That fits with a report by the Airlines Reporting Corp, which processes ticket transactions for airlines and more than 9,400 travel agencies, including websites such as Expedia and Orbitz. That 2012 study found that the optimal time to book is about six weeks in advance. Fliers booking then paid about 5.8 percent less than the average domestic fare.

Now, here’s some bad news: The formula is completely different for those peak travel periods when everybody wants to fly. So, if you still haven’t booked your flights to Europe for this summer, forget about it. The best time to buy those, according to ChaeapAir, was a whopping 319 days in advance.

But at least you can start thinking now about Thanksgiving and Christmas travel. The cheapest day to book those flight last year was June 4 – roughly five and six months prior to the respective holidays.

Here are some other tips to saving:

Look for connecting flights. Flying nonstop is ideal, but that convenience isn’t free. Adding one stop could save $100 round-trip. Just leave plenty of time to connect.

Be flexible with your dates. Use a flexible date search to find the cheapest days in a month to fly.

Consider the 24-hour rule. U.S. airlines are required to let you cancel most tickets booked directly though their websites. (There are exceptions for those within a week of travel.) After you book, check the next morning and see whether the price fell. If so, call to cancel and rebook.

After 24 hours, still watch for price declines. Any savings is typically wiped out by fees to change your reservation, ranging from $75 to $200. However, Southwest Airlines doesn’t impose change fees and Alaska Airlines waives them up to 60 days before a trip. If fares on those carriers drop, you can get a credit for the difference.

Book intra-Europe flights through overseas websites. The flights are often cheaper on the airline’s home country website. Google’s Chrome browser will translate it for you. If that doesn’t work, try the country specific site of Expedia.

Pick two different airlines. Most airlines now sell one-way flights at reasonable prices. One airline might be cheaper for the outbound flight and another for the return.

Search multiple sites. The cheapest flight doesn’t always show up on every website. Expedia, Orbitz and Travelocity are the biggest online ticket-sellers. Sometimes better deals can be found on Kayak, Hipmunk, AirfareWatchdog, Yapta, FareCompare, CheapOair, Mobissimo and Fly.com. Some airlines, like Southwest, aren’t included on many sites. Look at airport websites to make sure you aren’t missing a carrier.

Fly, then drive. Some airlines have a virtual monopoly at certain airports, allowing them to charge more. Most search sites can check fares at airports 50, 75 or even 100 miles from your destination.