Trek tends to fudge the dates on purpose. It's supposed to take place In The Future, and you're supposed to be thinking about whatever philosophical concept the episode is about, not the exact timeline of events. From Wikipedia: "stardates were originally intended to avoid specifying exactly when Star Trek takes place." I hate linking to Fandom wiki pages, but I'll say the page on stardates goes on at length about how inconsistent Trek time is.
Jumping around within a single episode is a little funny, but doesn't surprise me. Some writers try to be consistent, but maybe only for a few connected episodes, and some don't try at all. Sometimes a character will die, but an earlier episode or a flashback with a clearly later stardate will see them alive. There's all kinds of technobabble about where they are in the universe, and light speed relativity, and so on, but at the end of the day the show isn't trying to hide any serious messages in its timeline so long as the story of the episode makes some level of sense.