Generally I will casually ask something like "What time were you planning on heading out?" in a conversation. This usually prompts them to respond and ask something like "Why, do you have to be somewhere?", at which point you can bring up leaving.
If that doesn't work there's always the classic move of texting a friend and asking them to call you in 5 minutes about some "emergency".
Those are the best ways in my opinion, but you could also just modify the first method to be more direct, saying "By the way, I have somewhere to be at 4."