Yes, you need to buy shares before the ex-dividend date to be eligible to receive the dividend.