For the first time since World War II, the federal government in May spent more than twice as much money in a month as it took in.

In the process, the Treasury Department reported yesterday that it set a new record for a monthly budget deficit, $40.5 billion.

The red ink, swollen in part by unusually late income tax refunds, easily eclipsed the old record of $33.9 billion set in May 1984. There was an additional $1.5 billion deficit in so-called off-budget accounts, bringing the total deficit for the month to $42 billion.

A record also was set for the first eight months of a fiscal year, with a cumulative deficit of $156.2 billion, compared with $140.2 billion last year. Adding the off-budget items raises the total deficit for the first two-thirds of the year to $160.7 billion.

The administration projects a budget deficit of $203 billion for the entire year, plus another $10.3 billion in the off-budget accounts, for a total of $213.3 billion. The $203 billion would compare with the record annual deficit of $195.4 billion in fiscal 1983 and $175.4 billion in 1984.

Spending so far this fiscal year is up nearly $70 billion from the same period in 1984, to a total of $630.8 billion. Among the outlay categories, spending for national defense was $22.2 billion in May and those for Social Security and Medicare were next at $21.4 billion.

The lead is bouncing back and forth between those two categories, but by the end of this fiscal year in September, defense will have first place locked up, according to budget projections. Spending for Social Security and Medicare have been higher than defense since 1974.

The third-largest spending category for the month was $11.5 billion in net interest payments on the national debt, closely followed by $10.7 billion for incomes security spending, a category covering programs such as supplemental security income payments for the elderly poor and aid to families with dependent children.

At the end of May, the government owed just over $1.8 trillion, with slightly more than $300 billion of the total borrowed from various government trust funds, primarily those for Social Security.