Melatonin (particularly at higher doses) has a fast-acting sedative effect, so if you want to use it to help you fall asleep, then taking it 30 minutes before bed makes sense.
However, if you want to use it to advance your circadian rhythm (i.e., be inclined to be more of a morning person), then it has the greatest effect when taken 9–11 hours before your sleep midpoint (so very roughly at 6 p.m.). Below is a graph of the effect it has on the circadian rhythm.
Melatonin (particularly at higher doses) has a fast-acting sedative effect, so if you want to use it to help you fall asleep, then taking it 30 minutes before bed makes sense.
However, if you want to use it to advance your circadian rhythm (i.e., be inclined to be more of a morning person), then it has the greatest effect when taken 9–11 hours before your sleep midpoint (so very roughly at 6 p.m.). Below is a graph of the effect it has on the circadian rhythm.
^https://www.timeshifter.com/jet-lag/melatonin-for-jet-lag-type-dose-timing