Matt Sullivan (Josh Hartnett) and his roommate, Ryan (Paulo Costanzo), are co-workers at a San Francisco dot-com company. Matt is obsessed with his ex-girlfriend, Nicole (Vinessa Shaw), and his obsession repeatedly causes him problems during attempted one-night stands. He has trouble moving on, since Nicole broke up with him. He confides his sexual problems to his brother, John (Adam Trese), who is training to become a Catholic priest. In an attempt to fix his problems, Matt vows to abstain from sexual stimulation, including masturbation, for the 40 days and 40 nights of Lent. John warns Matt that chastity is not easy; meanwhile, Ryan starts a popular office pool to bet on how long Matt can last.
Matt meets Erica (Shannyn Sossamon), a cyber nanny, and they begin to date. They face many challenges in their relationship, including her discovery of his celibacy vow and Matt's continuing feelings for Nicole. Matt's co-workers make many unsuccessful attempts to persuade him to have sex in order to win the pool, and as the days pass Matt's obsession with sex grows. At one point he angrily grabs a pornographic magazine from an office desk and begins to march towards a toilet stall in order to masturbate, but his co-workers stop him and convince him to maintain his pledge.
Despite the range of increasingly powerful cultural incentives to sex and orgasm surrounding Matt—scantily-dressed women, billboards, et cetera—Matt's commitment holds, and begins to frustrate a lot of the people around him who had fully expected him to break it long before he could get close to his goal. In the meantime, Erica and Matt are falling in love, and they plan a special encounter for the 40th night to celebrate his successfully completing his vow. On the 40th day, a newly single Nicole learns of the betting pool, makes a large bet and then rapes Matt while he is asleep.
Erica subsequently believes Matt dishonored his vow and was unfaithful to her, but Matt wins Erica back by reminding her of the special moments they shared during their relationship. The two reconcile in Matt's bedroom for many hours, with his co-workers making a new betting pool on the duration of their stamina. Upon seeing this, he kicks them out of his apartment and shuts the door.
40 Days and 40 Nights was filmed on location at Potrero Hill, San Francisco, California.
The film received mixed reviews. Rotten Tomatoes gives the film a score of 38% based on reviews from 134 critics. Metacritic gives the film a score of 53, based on reviews from 33 critics.
Roger Ebert of Chicago Sun-Times gave the film 3 out of 4 stars. He praised director Michael Lehmann for raising the film above the level of sexual sitcom, through his sympathy for his characters and use of humor to examine human nature. He also credited writer Rob Perez for dialogue about sex with "more complexity and nuance than we expect". The rape scene at the end of the film has been criticized by reviewers. Roger Ebert was not explicit in his description, but stated "Nicole's entire participation is offensive and unnecessary... there was a sweeter and funnier way to resolve everything."
The film earned a worldwide total of over $95 million.
In 2005 Empire magazine included the film on its list of "Worst Sex Scenes".