Have you ever made a wisecrack about something terrible that just happened, and been greeted with dead silence instead of a laugh? Not a great feeling.

It's tricky knowing what you're allowed to joke about, and what's off limits. After all, psychologists say humor is a great coping tool that acts like a "psychological immune system." But the timing of tragedy-related joking is key.

Making light of disaster too close to the actual event can cause your joke to bomb, and earn you a disappointed chastisement like, "Too soon, man." On the other hand, waiting too long can also take the edge off your one-liner and doom it to unfunniness. Thankfully, psychologist Peter McGraw, who directs the Humor Research Lab (delightfully shortened to HuRL) at the University of Colorado, has figured out the "comedic sweet spot" where "too soon" ends and tragedy becomes fair game laughs.

As Hurricane Sandy approached the eastern U.S. in fall 2012, McGraw and colleagues created a joke Twitter account, @AHurricaneSandy, and wrote a few tweets from the storm's point of view, like "Oh Shit just destroyed a Starbucks. Now I'm a pumpkin spice hurricane," and "JUS BLEW DA ROOF OFF A OLIVE GARDEN FREE BREADSTICKS 4 EVERYONE." Starting the day before the storm hit and continuing for several months after, they surveyed thousands of people and had them rate the tweets as humorous, upsetting, offensive, boring, irrelevant, or confusing, and then plotted the reactions over time.

Just before the storm made landfall, the tweets were generally seen as funny. Over the next week or so, though, "funny" ratings dropped and more and more people called the tweets upsetting and offensive. In the weeks after the storm, funny ratings stayed down and hit their lowest point in mid-November. After that, the tweets' funniness began to climb, and peaked 36 days after the storm made landfall. Eventually, funniness made another slow decline.

McGraw and his team say this all has to do with psychological distance. "One day before landfall, the tragic nature of the storm was unknown and thus hypothetical," so people felt okay laughing about it. As people learned about deaths and injuries, billions of dollars in property damage, and people left homeless, the tragedy became more real, and the tweets became less funny and more offensive. With time, people gained some distance from the disaster and it became funny again, until, the researchers say, too much distance made the jokes "tame and uninteresting."

While they only looked at the factor of time in this experiment, the researchers say that psychological distance can also come from being physically and socially further away from a tragedy. For instance, a Sandy joke is probably less offensive the farther you get from the East Coast. Someone's fly being down is funny as long as it's not yours.

"Avoiding a 'too soon' comedy fail or a 'too late' comedy dud," the researchers conclude, hinges on distance. There's a saying that "comedy is tragedy plus time," but this study shows that it can't be to little or too much time. Thirty-six days, it seems, should do the trick.