A magnesium deficiency can cause leg cramps and frequent leg cramps at night.
So by increasing your intake of magnesium can help prevent leg cramps that occur both during the day and night.
You can increase your magnesium intake by taking magnesium supplements or by eating foods that are rich in magnesium such as nuts, lentils, and quinoa.
Staying hydrated and doing leg stretches and taking a walk before going to bed can help prevent nighttime leg cramps.