He served on the original USS Enterprise from 2266 to 2270 (first five-year mission after the retirement of Dr. Mark Piper), then again from 2273 to 2278 (second five-year mission). He remained with the ship after it was turned into a cadet training vessel, up to it's destruction in 2285. He served on the Enterprise-A from 2286 to 2293. It makes a total 16 years he spent on the vessel, 23 if we count in the cadet training days too.
In the Klingon Empire "show trial" of McCoy and Kirk in Star Trek VI: The Undiscovered Country, McCoy testifies that he has been Chief Surgeon of the U.S.S. Enterprise for twenty-seven years. Perhaps he had been assigned to the Enterprise while he was do his Starfleet degree at Starfleet Academy after medical school, assuming he attended the Academy in the original prime timeline ProfessorTrek 20:58, August 6, 2011 (UTC)