A few weeks ago, I was reading stuff on FanFic dot com. It annoys me when I read a fiction story and see "At age 28, he was the youngest Captain ever." Go to the next story, and the main character there was the youngest at age 27. Two stories later, someone else made their character a Captain at 26. I'm waiting to see the story in which teen-agers are commanding starships. {sigh}
Using real-world numbers, one cannot go to Academy or West Point until age 17. It takes four years to graduate ... none of this "Four years? I'll do it in three!" So an officer can be a 2Lt / Ensign at age 21. It takes two years to make 1Lt / LT-jg and another two for Capt / LT. That's pretty much set in stone.
Now, to move up to Major / LtCmdr and then to LtCol / Cmdr and again to COL / CAPT, the LAW requires three years time in grade for each promotion. Of course, no one is ever promoted at the minimum time, but let's say somehow someone does. They will be 17+4+2+2+3+3+3 = 34 years old when they make O-6 Captain.
In theory, you can have a child prodigy get his degree by age 18 and then get a direct commission via OTS / OCS. That shaves off three years, and it is legal to promote from 2LT to 1LT in 18 months vice 24. Ergo, the absolute youngest a person in modern-day US military can theoretically make O-6 is age 30-1/2 years.