After a rocky couple of years, the stock market has been surging in recent weeks. The S&P 500 is up by more than 12% since late October alone, and it's soared by just over 20% since the beginning of the year.

While this has many investors feeling optimistic about the future, others are concerned that they've missed their best chance to buy. Many stocks are more expensive now than they were just a couple of months ago, and investing now could mean paying a premium.

With prices still on the rise, is it too late to invest in the S&P 500? And should you hold off on investing in case stock prices fall again soon? The answer could surprise you.

Person sitting at a desk looking at a laptop.

Image source: Getty Images.

When is the right time to invest in the stock market?

The market has been volatile over the past couple of years, and it's tempting to try to invest at just the right moment. If you're able to invest when prices are at their lowest and then sell when the market peaks, you could make a hefty profit.

However, timing the market accurately is incredibly difficult -- even for the experts. Nobody knows what the market will do in the short term, so it's impossible to know when stock prices have bottomed out or whether they'll fall again in the coming weeks.

Over the long term, though, the market is overwhelmingly consistent. Despite experiencing plenty of crashes, corrections, recessions, and other downturns, the S&P 500 has still earned positive total returns over decades.

In the past two decades alone, the market has faced everything from the dot-com bubble burst to the Great Recession to the COVID-19 crash and the most recent slump, along with countless smaller downturns along the way. Yet the S&P 500 is still up by nearly 215% since 2000.

^SPX Chart

^SPX data by YCharts

Over the long haul, there isn't necessarily a bad time to buy. Even if you had invested when prices were at their peaks immediately before a downturn, by simply holding your investment for a few years, you'd still have recovered your losses and gone on to see positive total returns.

This is a consistent trend with the S&P 500 throughout history, too. Analysts at Crestmont Research looked into the S&P 500's rolling 20-year total returns to see how many of those 20-year periods ended in positive total returns.

They found that every single period in the index's history resulted in positive gains. In other words, if you had invested in an S&P 500 index fund or ETF at any point and held it for 20 years, you'd have made money -- even if the market was extremely volatile in that time.

The key to keeping your money safe

Investing in an S&P 500-tracking fund is one of the simplest and most effective ways to keep your money safer. The index itself has a long history of earning positive returns over time and recovering from downturns. While there are never any guarantees when it comes to investing, opting for an S&P 500 index fund or ETF is about as close to guaranteed long-term returns as you can get.

It's not the only way to build wealth in the stock market, however. If you choose to invest in individual stocks, you can potentially earn far more than you could with an index fund. It's crucial, though, to ensure you're investing in the right places.

Not all stocks will be able to recover from downturns, but the ones with healthy fundamentals (such as a competitive advantage, strong financials, and a knowledgeable leadership team) have the best chances. Even if the market takes a turn for the worse, these types of stocks are the most likely to recover.

When the market is surging, it can be tough to know when to invest. The good news, though, is that there's never necessarily a bad time to buy stocks. As long as you're investing in the right places and keeping a long-term outlook, right now is a fantastic time to invest in the stock market.