I'm assuming the word "hatching" in your post means that your alien embryos develop inside eggs which are themselves exposed to the environment, like non-mammals on Earth.
The egg shell could be transparent or translucent and lined with photosensitive biomolecules on the inner side. These molecules have one stable configuration and one metastable configuration with a lifetime of a few minutes. During the day, incident photons cause a transition from the stable state to the metastable state. At the end of the day, the amount of light received by these molecules drops to nearly zero and the molecules return to the stable state.
The embryos feed from the photosensitive molecules, and which metabolic pathway the molecules follow inside the embryos depends on state the molecules are in. The end-product of the sable-state molecule's metabolism is molecule s, and the end-produce of the metastable-state molecule's metabolism is molecule m. Thus, during the day the embryo has a lot more m and at night it has a lot more s.
Now, the embryos may have allele N or allele D (or both) of a certain gene which is responsible for producing a certain protein with two variants, S and M. These proteins are both useless during the embryo stage. However, the activator for N turns out to be s and the activator for D turns out to be m. Thus, during the day the embryo has more M and at night it has more S. By a remarkable evolutionary coincidence, these proteins have a lifetime of T/2 (i.e. the probability that they have been decomposed after a time T/2 is 50%), where T is however long twilight lasts in your aliens' homeworld. Thus, during twilight the amount of one protein slowly diminishes while the amount of the other protein increases: both proteins are simultaneously present inside the embryo.
At birth, a baby alien must start feeding off something else (other aliens, mother's milk, whatever), and it is born hungry, so it must take its first meal within minutes of being born. The new diet is compatible with both metabolic pathways but changes the baby alien's metabolism: the s metabolic pathway now relies on protein S and produces a molecule called s2, while the m pathway now relies on M and produces molecule m2. The molecules s2 and m2 are powerful neurotransmitters which determine whether the alien is nocturnal or diurnal (respectively). It also turns out that s2 is an inhibitor of the m pathway and m2 is an inhibitor of the s pathway, so once the alien has had its first meal its metabolism stops for a while. s2 and m2 are also highly reactive with each other, forming a useless compound which is simply excreted by the baby alien. Eventually, whichever molecule (s2 or m2) is present at a higher concentration in the body is the last one standing, so the other metabolic pathway is permanently shut off, at which point the alien is defined as either diurnal or nocturnal. If it has roughly equal amounts of s2 and m2 (because it was born at dusk or at dawn), it is impossible to tell a priori which molecule will win (and the "battle of the molecules" takes a little longer than usual to settle, but it always does eventually because the probability that the alien has exactly the same number of m2 and s2 molecules in its body is nearly zero).
Of course, if the alien only has one allele, only one of the proteins will be produced and only one of the modified metabolic end-products (s2 or m2) will be produced regardless of whether the embryo is born during the day or during the night.
(Note that it is only the photosensitive biomolecule in the inner lining of the egg that has a metastable state; m, m2, M, s, s2 and S are all stable.)
So:
- DD: only protein M is produced => only molecule m2 is produced => diurnal
- NN: only protein S is produced => only molecule s2 is produced => nocturnal
- DN, born during the day: M is present in higher quantities than S => more m2 is produced => only m2 is left in the end => diurnal
- DN, born at night: S is present in higher quantities than M => more s2 is produced => only s2 is left in the end => nocturnal
- DN, born at twilight: both M and S present in roughly equal quantities => can be diurnal or nocturnal
Sorry for the weird nomenclature. Hopefully that wasn't too confusing. :)
Some interesting scenarios which come to mind if you go for this explanation:
- A DN alien born at twilight who is for any reason unable to consume the appropriate food immediately but somehow doesn't starve to death would be neither diurnal nor nocturnal (it would either be awake all day or sleep all day, depending on whether m2 and s2 are promoters or inhibitors of neural activity, respectively) until its first "proper" meal, and the time of said meal (as opposed to the time of birth) would determine whether it is diurnal or nocturnal.
- An egg which has been inside a cave, a very dense forest, a building or similar during the last few hours before hatching is equivalent to the egg hatching at night. If said building has artificial lighting, then it could be equivalent to the egg hatching during the day (if the light which causes the photosensitive biomolecules to switch to the metastable state is visible light) or still equivalent to it hatching at night (if it is UV light or something else not available via artificial lighting).
- Following the previous idea, in a society with enough scientific knowledge concerning what determines whether an individual is diurnal or nocturnal, parents (or malevolent individuals) could force an alien to be diurnal or nocturnal (whichever suits their needs) by placing the egg in an appropriate place before it hatches.
- An alien who is diurnal could switch to nocturnal by injecting (or consuming) lots and lots and lots of s2 (or s), and an alien who is nocturnal could switch to diurnal in a similar fashion. If you want, you can make this highly dangerous due to reactions between m2 and s2 (causing mutual annihilation and thus a sharp decrease in neural activity while both neurotransmitters are present), due to reactions of s2/m2 with other neurotransmitters which also differ between diurnal and nocturnal individuals (causing whatever symptoms you can think of), or simply due to psychological factors.
Hope this helps!