The stock market has been unexpectedly soaring after its long slump in 2022, with the S&P 500 up by more than 25% from its lowest point last October.

Many people have been quick to call this the start of a new bull market, as the index has risen by more than 20% from its low. Others, however, claim that until the S&P 500 reaches a new all-time high (a feat it's inching closer to by the day), this isn't a bull market just yet.

Either way, while many investors have been pleasantly surprised at this surge, others may be wondering if they missed their best opportunity to buy earlier in the year when prices were lower.

With prices continuing to rise, is it too late to invest in the S&P 500? Or should you still buy now? The answer is simpler than you might think.

Timing isn't as important as it may seem

While it may sound counterintuitive, it doesn't necessarily matter when you invest or what the market is doing at the moment. What matters is how long you stay invested.

Maintaining a long-term outlook is key to generating wealth in the stock market, and if you're a long-term investor, the near-term ups and downs won't have as much of an effect on your total savings. Even if you invest at a seemingly terrible moment, you can still see significant long-term gains.

For example, say you had invested in an S&P 500 index fund in September 2009. The market was well into recovery mode after the Great Recession at that point, with the S&P 500 officially bottoming out in March of that year.

At the time, it may have seemed like you'd missed the best moment to buy. By September, the S&P 500 had already increased by close to 50% from its low point in March. But if you'd invested anyway and simply kept a long-term outlook, you'd still have seen returns of nearly 200% over the next 10 years.

Chart showing the S&P 500 rising overall since 2010.

^SPX data by YCharts

Could you have earned more if you'd invested at the market's lowest point in March? Of course. But hindsight is 20/20, and in the moment, it's impossible to know when the market has reached its highs or lows. If you're not investing at all because you're waiting for the perfect time to buy, you're only hurting your long-term earnings potential.

An important caveat

There's never necessarily a bad time to buy if you're a long-term investor. However, the key to ensuring your portfolio thrives over the long run is to invest in the right places.

Not all stocks will be able to survive market downturns or see consistent growth, and if you invest in less-than-stellar companies, it could cost you.

While there are never any guarantees when it comes to investing, the companies that have the best chance of earning positive returns over time are the ones with healthy underlying business fundamentals. This includes everything from strong financials to a competitive advantage in the industry to a competent leadership team.

These types of stocks will still experience short-term slumps (especially when the market is volatile), but they're far more likely to rebound from those downturns and go on to see positive total returns. The more of these stocks you have in your portfolio, the safer your money will be.

With stock prices on their way up, it may seem like you've missed the best opportunity to buy. But when you're a long-term investor, any time is a smart time to invest. By buying now and investing in the right places, you'll be setting yourself up for potentially significant long-term gains.