As the first nominating contest approaches, Hillary Clinton’s commanding lead nationally in the Democratic primary has largely melted away, according to the latest New York Times/CBS News Poll.

The tightened race between Mrs. Clinton and Senator Bernie Sanders of Vermont is revealing a sharp generational divide within the Democratic Party, with primary voters under 45 favoring Mr. Sanders by a roughly 2-to-1 ratio.

Yet more than 7 in 10 Democratic voters — including most supporters of Mr. Sanders — still believe Mrs. Clinton will ultimately win the party’s nomination. Voters expressed deeper confidence in her ability to be an effective commander in chief and more of her supporters say their minds are made up compared with Mr. Sanders’s backers.

Over all, 48 percent of Democratic primary voters across the country support Mrs. Clinton, while 41 percent back Mr. Sanders, the poll found. Just a month ago, she led Mr. Sanders by 20 percentage points nationally.

Mr. Sanders’s shifting fortunes underscore the unsettled state of the presidential race in both parties with just three weeks before the Iowa caucuses. Previous contests have seen candidates rise and fall in the weeks before the first votes are cast, and national polls at this stage of the race are not necessarily predictive of the final outcome of the monthslong nominating battle. But Mr. Sanders’s surge has clearly unnerved the Clinton campaign, and she is responding aggressively.

Image

Senator Bernie Sanders of Vermont is favored by a 2-to-1 ratio among Democratic primary voters under age 45, a New York Times/CBS News poll finds.CreditMax Whittaker for The New York Times

Two state polls released Tuesday further underscored the stiff competition that Mrs. Clinton is facing from Mr. Sanders. A Quinnipiac University poll found Mr. Sanders rising in Iowa, and a survey by Monmouth University gave Mr. Sanders a double-digit lead in New Hampshire.

“I like Bernie’s sincerity,” Dalton Paget, 27, an insurance agent from Spokane, Wash., said in a follow-up interview. “He’s talking about working towards policies that he’s been championing for a long time.”

In particular, Mr. Paget cited Mr. Sanders’s advocacy of overhauling the campaign finance system. “Honestly, before I heard much about Bernie Sanders and realized he had a decent chance for winning, I might have supported Hillary,” he said.

Mrs. Clinton is no longer treating Mr. Sanders as a distant rival who can be left unmentioned as she looks toward the general election. She is now confronting Mr. Sanders more forcefully, raising doubts about his electability and criticizing him as weak on the issue of gun violence.

In a rare issue on which she can challenge Mr. Sanders from the left, Mrs. Clinton has repeatedly sought to highlight his vote in 2005 for legislation that broadly shields gun manufacturers and dealers from liability lawsuits, portraying him as out of step with her and President Obama. And on Monday, she proposed a tax surcharge on people who earn more than $5 million per year, exploiting an issue that has energized Mr. Sanders’s supporters.

There is less apparent movement in the Republican contest, where Donald J. Trump maintains a sizable lead among Republican primary voters nationally. Thirty-six percent of them support him, compared with 19 percent for Senator Ted Cruz of Texas, and 12 percent for Senator Marco Rubio of Florida. Mr. Trump’s support is roughly the same as it was a month ago.

Only about a third of Republican primary voters who back a candidate say their minds are made up. But most Trump supporters — 52 percent — say they have decided. A narrow majority of Republican primary voters say they expect Mr. Trump to be the party’s nominee, similar to the finding of last month’s poll.

Mr. Trump and Mr. Cruz run closely among very conservative voters, but Mr. Trump now leads among evangelicals 42 percent to 25 percent.

“I think he’s going to try to do what’s best for the working man,” said Kevin Jarrell, 41, from Proctorville, Ohio, who said he was a born-again Christian. “He’s got money so he’s not going to try to do what’s best for him. He’s going to try his best to create jobs and he’s going to shut out the refugees.”

But Mr. Trump trails his top two rivals on whether he possesses the right kind of temperament and personality to be a good president. Half of Republican primary voters say he does, while about two-thirds say so of Mr. Cruz and Mr. Rubio.

The poll also offered the latest evidence of the continuing free-fall of Ben Carson. He is backed by just 6 percent of Republican primary voters in the latest poll, down from 13 percent a month ago and 26 percent in late October, when he topped the Republican field.

The generational divide on the Democratic side was more pronounced than even the split among voters of differing political ideology. In addition to his strong support among younger voters, Mr. Sanders, who describes himself as a democratic socialist, is backed by 55 percent of liberals, compared with 40 percent who support Mrs. Clinton.

Mrs. Clinton is boosted by older voters, who favor her by an even larger margin than younger voters favor Mr. Sanders. She fares better with moderates than Mr. Sanders does; 51 percent support her, while 33 percent back Mr. Sanders.

She also holds a double-digit lead among female voters, but the race is closer among men — 45 percent back Mr. Sanders, compared with 40 percent for Mrs. Clinton.

“I like the way he says the economy should be run,” said Jerry Auguste, 28, of Brooklyn, who works in retail. He said he was more with Mrs. Clinton at first, but has been leaning toward Mr. Sanders in the past month or so, “mainly because I’ve heard more of what he’s been saying about the economy and climate.”

About as many Democratic primary voters say Mr. Sanders shares their values as say Mrs. Clinton does. But Mrs. Clinton, who served as secretary of state in Mr. Obama’s administration, holds an edge in an important area: Some Democratic voters seem to have an easier time envisioning her in the White House.

More than 4 in 10 say they are very confident in her ability to be an effective commander in chief of the nation’s military, compared with just over 2 in 10 who are very confident in Mr. Sanders’s ability.

“As far as commander in chief goes, she’s been there, she knows what’s been going on, and she would be effective,” said Leona H. Alford, 81, a retiree from Brown Deer, Wis., who worked in social services.

Likewise, about 8 in 10 Democratic primary voters say Mrs. Clinton has the right kind of temperament and personality to be a good president, compared with two-thirds who say Mr. Sanders does.

Former President Bill Clinton, who has started campaigning on his wife’s behalf, is highly popular with the voters she is courting, with 7 in 10 Democratic primary voters viewing him favorably.

About 3 in 10 say his involvement in Mrs. Clinton’s campaign makes them likelier to support her, though two-thirds said it does not make a difference.

The national Times/CBS News poll, conducted Jan. 7 through 10 on cellphones and landlines, included 442 Republican primary voters and 389 Democratic primary voters. The margin of sampling error is plus or minus 6 points for both Democratic and Republican primary voters.

A version of this article appears in print on , Section A, Page 12 of the New York edition with the headline: Clinton Loses Ground to Sanders in New Poll as Trump’s Lead Holds. Order Reprints | Today’s Paper | Subscribe