You generally need to buy one day before Ex-Div and hold it through the Record Date, for a total 3-day trading settlement time. Ex-Div means "without dividend", as in, the date when you no longer get this month's div. It's usually the first business day of the month.
Yeah you wouldn't want to buy just before the dividend date, unless you plan to hold for awhile. If you just plan to hold through the record date, it will almost be a wash. You might make a little bit if you're lucky. Gotta hold these for at least 3-6 months to assure you get a positive return.