WASHINGTON — As a candidate, President Trump disparaged NATO as a musty relic of old thinking, an alliance focused on long-gone adversaries rather than new-era threats, a burden that drained American resources on behalf of ungrateful partners who did not pay their share. In a word: “obsolete.”

That was then. After 82 days in office, Mr. Trump officially pronounced NATO rehabilitated, taking credit for transforming it into a modern, cost-sharing, terrorism-fighting pillar of American and European security. “I said it was obsolete,” the president noted on Wednesday as he hosted
NATO’s secretary general. “It’s no longer obsolete.”

Never mind that the alliance has changed very little if at all in the last three months, and that whatever modest changes have been made were in train long before Mr. Trump entered the doorway of the White House. After weeks of being lobbied, cajoled and educated by the leaders of Britain and Germany, not to mention “my generals,” as he likes to call his national security team, Mr. Trump has found fresh virtue in a venerable organization.

“Nothing has changed at NATO in the last 80 days,” said Ivo Daalder, a former ambassador to NATO under President Barack Obama and now president of the Chicago Council on Global Affairs. “It’s gratifying to see the president affirming that NATO is ‘no longer obsolete’ — it never was.

Perhaps the one important thing that has changed in the last 80 days is that as president, Mr. Trump has come to appreciate the importance of this alliance and how it contributes to security and stability in Europe and beyond.”

Mr. Trump’s about-face on NATO was only part of a day of flip flops at the White House. Within a matter of hours, the president determined that China is not a currency manipulator after all, embraced the Export-Import Bank that he once called unnecessary and suggested he might keep Janet Yellen, the Federal Reserve chairwoman he said he would replace after her term expired. Most striking, he pivoted 180 degrees on Russia, lashing it for supporting rogue nations after years of praising President Vladimir V. Putin.

The Russia reversal and the NATO turnabout were inherently linked, of course. As Russia appears more ominous, NATO seems more necessary. But the shift in attitude also offered one of the starkest examples yet of Mr. Trump’s evolving views on domestic and foreign policy, as the first president ever elected without political or military experience settles into the role of commander in chief.

“We must not be trapped by the tired thinking that so many have, but apply new solutions to face new circumstances, and that’s all throughout the world,” Mr. Trump said at his news conference with Jens Stoltenberg, the NATO secretary general.

The president’s support for NATO heartened Mr. Stoltenberg and European leaders who one after the other have tried to impress upon Mr. Trump the value of the alliance, especially Prime Minister Theresa May of Britain and Chancellor Angela Merkel of Germany.

“In a more dangerous and more unpredictable world, it is important to have friends and allies,” Mr. Stoltenberg said at the White House on Wednesday. “And in NATO, America has the best friends and the best allies in the world.”

Mr. Trump’s campaign criticism of NATO stunned many at home and abroad, especially when he suggested conditioning America’s commitment to defend its treaty allies on whether they had met their financial obligations. Just days before taking office, he dismissed the alliance as “obsolete.” No new American president had ever come to power expressing such disdain for NATO.

Mr. Trump’s shift on NATO has been a stutter-step transition. When he hosted Mrs. May a week after taking office in January, she turned to him at a joint news conference afterward and tried to put on the record what he had told her in private. “You confirmed that you’re 100 percent behind NATO,” she told him. He did not contradict her but he did not confirm it, either.

Even as Vice President Mike Pence traveled to Europe a few weeks later to reassure allies, Mr. Trump back home pressed his case that NATO allies do not spend enough on their own defense. “Many of these countries are very rich countries,” he complained at a rally in Florida. “They’re not paying their bills. They’re not paying their bills.” When Ms. Merkel visited last month, a British newspaper reported that Mr. Trump asked her to pay back unmet defense obligations from the last 15 years, forcing the White House to deny that he actually presented her with an invoice.

Mr. Trump’s drumbeat about spending by allies may be having an effect. He is simply repeating the same grievance lodged by other presidents, including Mr. Obama. But Mr. Trump does it louder and more insistently, forcing the issue to the front of the agenda in a way that Mr. Obama’s polite nudging did not.

Only five of the 28 members of NATO met their target of spending 2 percent of their gross domestic product on defense last year in accord with a 2014 agreement during Mr. Obama’s tenure, but Mr. Stoltenberg reported that the number would rise to eight by next year.

“President Trump is no different” from other presidents in complaining about allied spending, but “he has been far more aggressive in his statements and frankly, I think that’s had some effect,” said Joseph W. Ralston, a retired Air Force general and former NATO supreme allied commander.

Mr. Trump’s other main criticism of NATO appears to have had less impact. During the campaign, he said the alliance did not fight terrorism, which was not exactly the case. NATO has had troops in Afghanistan fighting terrorists since shortly after the attacks of Sept. 11, 2001, and has provided assistance to Iraq in its own war against extremists.

Without directly contradicting Mr. Trump, Mr. Stoltenberg in his very diplomatic way reminded the president of that on Wednesday.

“Allies sent Awacs surveillance planes to help patrol American skies and we launched NATO’s biggest military operation ever in Afghanistan,” Mr. Stoltenberg said. “Hundreds of thousands of Europeans and Canadian soldiers have served shoulder to shoulder with American troops. More than 1,000 have paid the ultimate price. Our mission in Afghanistan is a major contribution to the fight against international terrorism.”
Still, he agreed “that NATO can and must do more in the global fight against terrorism.”

Mr. Trump claimed credit for turning the alliance around on terrorism. “I complained about that a long time ago and they made a change and now they do fight terrorism,” he said.

But he did not explain what change he meant and a subsequent inquiry to the White House for elaboration went unanswered. NATO created a new intelligence division headed by an assistant secretary general last year, but the idea predated Mr. Trump.

“It’s to better deal with hybrid warfare and crisis management, not just terrorism, but it will help in C.T. as well,” Alexander R. Vershbow, who served until last year as deputy secretary general of NATO, said, using initials for counterterrorism. There has been “nothing really new on terrorism beyond this.”

Still, NATO supporters welcomed the turnaround. Just this week, Mr. Trump signed ratification papers clearing the way for Montenegro to join as the alliance’s 29th member — over Russian objections — and he plans to make his first foreign visit as president to the NATO summit meeting in Brussels next month.

Mr. Ralston, the former NATO commander, said Mr. Trump’s criticisms have done some good but the president is now gaining a fuller understanding of NATO from his own team, which has considerable experience with the alliance and includes Defense Secretary Jim Mattis and Lt. Gen. H.R. McMaster, the national security adviser. Mr. Ralston recalled that John F. Kelly, the retired Marine general who is now secretary of Homeland Security, served as his special assistant at NATO.

“The president is getting advice from different people now than he did in the campaign,” Mr. Ralston said. “These guys are very good and I’m sure they’re giving their unvarnished advice to the president and I think the president is learning from that.”