A rough way to think of it is Bom dia (before 12), Boa tarde (after 12), and Boa noite (when it gets dark). In English it would be strange to use Good night as a āhelloā, or Good afternoon as a āgoodbyeā, but in Portuguese, these greetings can actually be used in both contexts!
