system
Given the following post, respond as if you're commenting on Reddit:
human
Does pregnancy really last a set amount of time? For humans it's 9 months, but how much leeway is there? Does nutrition, lifestyle and environment not have influence on the duration of pregnancy?
gpt
There is a little leeway, but it's really constrained by biology. If the baby comes out too early, the lungs aren't developed enough to oxygenate their blood. They are often weak and can't latch onto their mom's breast and suck. They don't have much body fat, they have higher surface-area to mass ratio and have a hard time staying warm enough. 

If the baby is born too late, the placenta starts to degrade, decreasing the baby's supply of nutrients and oxygen. The baby is also more likely to pass its first poo (meconium) into the amniotic fluid and aspirate it, causing huge pulmonary problems. There's also the problem of the baby simply getting too big to pass through the pelvis. And mom's getting gestational diabetes and high blood pressure. 
C-sections haven't been around nearly long enough to remove those selective pressures. 

Before modern obstetrics, death rates for human moms and babies was astonishingly
 high compared to other primates, due in part to our pelvis being tipped for walking upright. 

We also have huge brains vs other primates. Humans are the result of an evolutionary balance between hips that can walk upright and intelligence. We are born relatively immature so our skulls can pass through our mothers' pelvises and finish maturing outside the womb. Intelligence is such an asset that it's worth having helpless, weak babies that need ~3 years to walk, talk and kind of fend for themselves.

Natural selection narrowed down the length of the perfect pregnancy to 38-41 weeks.

The window is actually probably smaller than 3 weeks, because we don't usually know the exact date of ovulation. We estimate it on last menstrual cycle, which can vary a lot.