When Sarah Silverman posts jokes about the Holocaust to YouTube, she gets millions of page views. When Prince Harry dressed up as a Nazi, he got blasted by the international press. Why can Sarah—but not Harry—get away with it? Comedians make an art of toeing the line between racy and funny, but for those of us who’ve ever wondered whether or not the world is ready for our witty take on the latest tragedy, science is here to help: psychologists have devised a formula showing the point at which it becomes funny to make a joke about a real-life disaster.

For a new paper in the journal Social Psychological and Personality Science, a team of psychologists led by University of Colorado—Boulder professor Peter McGraw looked at how people’s reactions to jokes about Hurricane Sandy changed over time. McGraw recruited participants at ten intervals, from the day before Sandy was expected to hit to four months after the event, and asked them to read tweets from the parody account @AHurricaneSandy:

Over the course of the study, over a thousand people weighed in, ranking each tweet on a one-to-seven scale and registering whether they found it funny, upsetting, offensive, boring, irrelevant or confusing.

“Humorous responses to Sandy’s destruction rose, peaked, and eventually fell over the course of 100 days,” wrote McGraw. “We find that temporal distance creates a comedic sweet spot. A tragic event is difficult to joke about at first, but the passage of time initially increases humor as the event becomes less threatening. Eventually, however, distance decreases humor by making the event seem completely benign.”

Before the hurricane hit, when the danger was still hypothetical, people thought the tweets were pretty funny: They gave them, on average, between 3 and 4 points on the 1 to 7 scale. But when Sandy arrived and people realized the extent of the damage it had wrought, humor declined, reaching a low point 15 days after Sandy’s landfall. As time passed, though, it became more and more acceptable to “find humor in the tragedy,” and people’s rankings climbed—reaching a peak 36 days after landfall. After this point, humor dropped off again, reaching a new low at Day 99.