Maybe he went into sugar shock over a Danish pastry.

The president of the United States, leader of the free world, standard-bearer for everything upright, good and wholesome about the nation he leads, lost his morality, his dignity and his mind, using the solemn occasion of Nelson Mandela’s memorial service Tuesday to act like a hormone-ravaged frat boy on a road trip to a strip bar.

In front of 91 world leaders, the mourning nation of South Africa and Obama’s clearly furious wife, Michelle, the president flirted, giggled, whispered like a recalcitrant child and made a damn fool of himself at first sight of Denmark’s voluptuously curvy and married prime minister, Helle Thorning-Schmidt.

Not to be outdone by the president’s bad behavior, the Danish hellcat hiked up her skirt to expose long Scandinavian legs covered by nothing more substantial than sheer black stockings.

With Michelle glowering, the world judging and mental fidelity floating into the abyss, the president leaned into the air space of the cross-legged Danish cupcake, who is known in Copenhagen as a fan of America’s randy TV show “Sex and the City.’’ It was the memorial equivalent of a bodice-ripper.

Thorning-Schmidt placed her hands dangerously close to Obama’s side. The president’s cackling head moved inches from the Danish tart’s and yards away from his wife’s. Obama then proceeded to absorb body heat from the Dane, which he won’t be feeling at home for a long time.

This wasn’t a key party. This was a service that filled an entire Johannesburg soccer stadium for Mandela, the great leader who freed his people out of racial oppression and died last Thursday at age 95.

He was a man who Obama compared at the memorial to Abraham Lincoln, Mahatma Gandhi and Dr. Martin Luther King. And here was the president, publicly behaving as if he was plotting his next hook-up.

It was unseemly.

Michelle frowned and looked as if she wanted to spit acid at the man she married, a good-time guy who humiliated her in front of their friends, the world and a blonde bimbo who hadn’t the sense to cover up and keep it clean.

Finally, Obama posed for an iPhone selfie with the Danish hottie and British Prime Minister David Cameron. Only after the damaging photo eruption did Obama get hold of himself and regain a dim memory of his marriage vows. He finally straightened his face and moved away from the gentle gams of the Danish object of his desire.

He leaned nearer to Michelle, who clearly wasn’t having any of her husband.

On Wednesday, Thorning-Schmidt saw nothing wrong with the public display of middle-aged lust. One Danish Facebook user called the antics of a president of 52 and a prime minister of 46 “frivolous and disrespectful.” Another said it was “extremely inappropriate and embarrassing.’’

But Thorning-Schmidt attempted to laugh off the whole thing, saying, “It was not inappropriate.’’ Not inappropriate?

Pairing a black suit and blue tie is not inappropriate. Giving your wife grounds for divorce might be seen as otherwise.

But people won’t soon forget the escapades of the people whose salaries they pay.

President Obama has some ’splaining to do. To the woman he married. To his daughters. To the people of South Africa. And to the scandalized folks here at home.

He owes the world an apology.