You can write a.m. and p.m. as lowercase letters with periods after them or as small capitals without periods. Either way, there should be a space between the time and the a.m. or p.m. that follows. Although small capitals used to be preferred, it's now more common to see lowercase letters followed by periods (a.m. and p.m.). I suspect this is because it’s so hard to make small caps on a computer.
This is quoted from my favorite podcast, Grammar Girl.