These letters are abbreviations of the words in latin am = before meridiem and PM = post meridiem. Meridiem in Spanish means noon. So am in Spanish means before the noon and p.m. means past the noon. Both are written with a dot followed by each letter to indicate that it is an abbreviation.
Abbreviations of meridiem and post meridiem, latinisms referred to the anterior and posterior to the noon hours: before noon and after noon. For example, " the incident took place at 5 a.m. pm. ( at 5 in the afternoon ) "