President Trump was reeling from one of his worst weeks ever: The novel coronavirus was killing Americans, wrecking the economy and subsuming him and his presidency.

But in the pandemic, Trump saw an opportunity to cast himself in a new role: “Wartime president,” as he later dubbed it. Aides noted that Trump was punctual for last Saturday’s White House task force meeting, donning a navy “USA” cap and — instead of simply watching as Vice President Pence and the assembled health officials briefed the public that afternoon, as he’d initially planned — joining them at the rostrum.

All week, Trump reveled in his newfound character — that of a crisis commander steering his skittish nation through battle with what he called an “invisible enemy.” He parried questions, barked orders and stood stoically by as he accepted praise, day after day, from his underlings for his “strong leadership” and “decisive actions.”

But on Friday, Trump faltered. He argued based on “just a feeling” that, despite no scientific evidence yet, an anti-malaria drug could cure the coronavirus. He complained that he has not been credited for fixing a nationwide testing system that clearly is still broken. And when asked what message he had for Americans who were scared, he lashed out.

“I say that you’re a terrible reporter,” Trump answered to NBC News correspondent Peter Alexander. “That’s what I say.”

Trump’s past seven days at the helm of the coronavirus effort illuminated his mercurial nature and underscored his difficulty overseeing the national response to a global catastrophe largely out of his — or any other leader’s — control.

Trump — whose moods often determine policy and are almost directly correlated to the vagaries of 24-hour news cycles — has been lapsing into his self-destructive ways even when aides stress the importance of steady leadership during a national emergency.

Fixated on his portrayal in the media, Trump has used this past week to try to rewrite history in hopes of erasing the public’s memory of him dismissing the severity of threat and bungling the early weeks of the administration’s response.

“I’ve felt that it was a pandemic long before it was called a pandemic,” Trump said Tuesday. Only five days earlier he had declared, “It’s going to go away,” and two days before that he had said, “It will go away. Just stay calm.

After the coronavirus was first detected in China and swept across Europe, and even after the first reported case in the United States on Jan. 21, Trump tried to wave off the danger. He was then in the throes of the impeachment battle and distracted by the Democratic presidential primaries. The president accused the media of perpetuating a hoax, arguing that news organizations were drumming up hysteria over the growing public health crisis as a way to hurt his presidency.

The nadir for Trump came March 6, when he visited the Centers for Disease Control and Prevention headquarters in Atlanta and appeared to make a mockery of the scientists’ warnings. He then decamped for the extended weekend to Palm Beach, Fla., where he played golf and hung out with friends at his Mar-a-Lago Club, which itself turned into a coronavirus petri dish.

Trump’s public posture began to shift, however, once the financial markets started to plummet. He was particularly taken with the numbers — not just the cratering Dow Jones industrial average but also the briefings he received from Vice President Pence, multiple times a day, with fresh data and figures showing how the virus could devastate the nation if left unchecked.

A new study released earlier this week by the Imperial College London — which projected that 2.2 million would die in the United States alone if no steps were taken to curb the outbreak — was particularly influential among Trump’s inner circle.

Trump also was influenced by his conversations with business leaders and wealthy supporters, who lit up the presidential phone line with angst and alarm over the Wall Street meltdown. Their message: Get it together. The world’s collapsing and you’re flaunting that you don’t care.

Trump then took a series of steps in quick succession to try to gain control over the spiraling crisis. He delivered a prime-time address to the nation. He banned travel from Europe. And he declared a national emergency.

Though Trump claims his Jan. 31 restrictions on travel from China as evidence that he always has taken the coronavirus seriously, one senior White House official said his March 11 announcement prohibiting most travel from countries in the European Union — a critical diplomatic ally and trade partner — helped truly underscore for Trump the severity of the crisis.

Trump was angry that his error-riddled prime-time Oval Office address to the nation, in which he announced the Europe ban, was widely panned, and frustrated that so few allies defended him on television the next day. But on March 13, a news conference in the Rose Garden — at which he announced a new testing website and new testing locations, both of which were half-baked at best — buoyed his spirits because he finally felt he had at least the illusion of control, aides said.

Officials also pointed to Hope Hicks — Trump’s former communications director and close confidante who recently returned to the White House after a stint in Los Angeles — as a calming presence who helped focus Trump.

Each day after the task force meets and before members present their latest message to the public, a small group retreats to the Oval Office to strategize about the news conference. The group includes whatever officials are speaking that day, as well as Pence, Hicks, White House chief of staff Mark Meadows, the vice president’s chief of staff Marc Short, and Trump’s son-in-law and senior adviser Jared Kushner. Hicks often offers tonal suggestions, helping steer Trump toward the sort of more measured language that his advisers have long been pushing.

On Monday, Trump adopted the far more serious tone that his advisers had encouraged. He echoed the guidance of infectious disease experts and offered direction about what people should and shouldn’t do. He advised against gatherings of more than 10 people, as well as discretionary travel, and urged whoever could work from home to do so. He even hit the pause button on his various feuds with Democrats and the media.

“My focus is really on getting rid of this problem — this virus problem,” he said Monday. “Once we do that, everything else is going to fall into place.”

Trump spoke of the coronavirus as if it were a foreign adversary at war, drawing parallels between the ways Americans are adapting their lives to adhere to social distancing guidelines to the sacrifices citizens made during World War II. Speaking about his own leadership, Trump said Wednesday, “I view it as, in a sense, a wartime president.”

Historian Michael Beschloss said Trump’s conception of himself as a wartime leader is potentially apt.

“The war metaphor is actually a good one if what it means is that the president is acting as a commander in chief does, which is trying to orchestrate all of the power of the federal government to solve the problem and to level with the American people,” Beschloss said. “But this is not a war against a foreign enemy. It is not military. Waging a war is not the same thing as fighting an illness.”

The president’s resolve, however, did not last. Trump has never demonstrated the ability to sustain discipline or message control over an extended period — frequently following fleeting periods of calm with bursts of seeming self-sabotage — and this week was no different.

On Thursday, Trump snapped at a reporter who began a question by stating that “the economy is essentially ground to a halt.”

“Thanks for telling us — we appreciate it,” Trump said, before adding, “Everybody in the room knows that.”

By Friday, Trump was in full tirade mode. Seemingly desperate for a miracle medicine, he kept on pushing an anti-malarial drug as a potential cure-all, prompting Anthony S. Fauci, the director for the National Institute of Allergy and Infectious Diseases, to gently offer a more nuanced view.

But even the normally placid-faced Fauci could barely contain himself when Trump referred to “the State Department or, as they call it, the ‘Deep State’ Department.” Fauci, standing just behind Trump’s left shoulder but still on camera, smirked and touched his fingertips to his brow to cover his face as he struggled to suppress a chuckle.

Other moments were less humorous. When Alexander, the NBC reporter, asked Trump what message he had for “Americans who are watching you right now who are scared,” Trump angrily attacked him as “a terrible reporter” and called it “a very nasty question.”

When Alexander later posed the same question to Pence, it was Trump’s No. 2 who offered the words one might ordinarily expect from a wartime president: “Don’t be afraid. Be vigilant.”