Keira Knightley and Steve Carell have all the time in the world to get to know one another. Unfortunately, that’s three weeks.

As “Seeking a Friend for the End of the World” begins, an asteroid big enough to destroy everything on Earth is hurtling toward us. Obviously the situation is ripe for comedy.

“As dark as it is, everything tragic is pretty funny,” says writer-director Lorene Scafaria, who also wrote 2008’s “Nick & Norah’s Infinite Playlist.” “The best comedy has some tragedy in it, and I knew I wanted to mix the two as much as I could. Really there’s so much to laugh at, I feel, in dark times.”

Dodge (Steve Carell) is sitting in a parked car with his wife (played by Carell’s real-life spouse, Nancy) when he hears the news: The final, “Armageddon”-like attempt to destroy the asteroid has failed. There are only three weeks left for Earth and the people on it. When the report ends, she looks at him and then, without a word, sprints away from the car.

His wife gone, Dodge must decide how to spend his last three weeks on his own. He tries returning to work and heading to his best friends’ end-of-the-world party, where some very normal middle-class people decide to try heroin.

Dodge’s downstairs neighbor, Penny (Keira Knightley), a Manic Pixie Dream Girl who has missed the last flight to see her family back in England, falls into his life just as Dodge is wondering if following all the rules has really gotten him anywhere. A picaresque adventure through the end times ensues, as does romance.

“I just thought, well, what if you just gave these people the loudest ticking clock ever?” says Scafaria, who says her inspiration for the film came from watching lots of disaster movies and wondering what the rest of the doomed Earthlings were doing.

“When you take forever off the table, it’s most interesting to see what two people who just met are gonna do with that.”