Use this link and scroll down to Photo Cell Circuits. Read about the LM 339 in some detail and get a grasp on Hysteresis. You will want hysteresis with an active light or dark detector in most cases, especially during a transition from daylight to darkness. There are literally dozens of circuits to do what you want, using a comparator is just one of many.