You need to buy the shares at least on the Cum dividend date and hold it till the Exdividend date. You will have to own shares for at least 2 days before you can sell and still earn dividend. You can sell shares on Record date and still earn the dividend.