According to the Wikipedia article "Affirmation", in the UK:
A right to give an affirmation has existed in English law since the Quakers Act 1695 (An Act that the Solemne Affirmation & Declaration of the People called Quakers shall be accepted instead of an Oath in the usual Forme; 7 & 8 Will. 3 c. 34) was passed. The text of the affirmation was the following: "I A.B. do declare in the Presence of Almighty God the Witnesse of the Truth of what I say".[1] The right to give an affirmation is now embodied in the Oaths Act 1978, c.19, which prescribes the following form: "I, do solemnly, sincerely and truly declare and affirm" and then proceed with the words of the oath prescribed by law, omitting any words of imprecation or calling to witness.
...
The cause for such a right is exemplified R v William Brayn (1678). William Brayn was charged with the theft of a horse from Quaker Ambros Galloway. Brayn pleaded 'not guilty'. One witness testified that the horse was owned by Ambros Galloway, and another witness said that he [probably Galloway] bought it from Brayn. As Galloway was a Quaker, he would not, "for conscience-sake", swear and so could give no testimony. The court directed the jury to find Brayn 'not guilty' for want of evidence and committed the Quaker "as a concealer of Felony" for "refusing an Oath to Witness for the King"
bout the US, the same article states:
The original 1787 text of the Constitution of the United States makes three references to an "oath or affirmation": In Article I, senators must take a special oath or affirmation to convene as a tribunal for impeachment; in Article II, the president is required to take a specified oath or affirmation before entering office; and in Article VI, all state and federal officials must take an oath or affirmation to support the Constitution. Another reference appears in the Fourth Amendment, which specifies that all warrants must be supported by evidence given under oath or affirmation.
Rule 63 of the Federal Rules of Evidence states:
Before testifying, a witness must give an oath or affirmation to testify truthfully. It must be in a form designed to impress that duty on the witness’s conscience.
and the notes to this rule read:
The rule is designed to afford the flexibility required in dealing with religious adults, atheists, conscientious objectors, mental defectives, and children. Affirmation is simply a solemn undertaking to tell the truth; no special verbal formula is required. As is true generally, affirmation is recognized by federal law. “Oath” includes affirmation, 1 U.S.C. §1; judges and clerks may administer oaths and affirmations, 28 U.S.C. §§459, 953; and affirmations are acceptable in lieu of oaths under Rule 43(d) of the Federal Rules of Civil Procedure. Perjury by a witness is a crime, 18 U.S.C. §1621.
Rule 6.10(b) of the Court Rules of North Dakota reads:
(b) Affirmation. A person must be allowed to make an affirmation instead of taking an oath, by substituting the word "affirm" for the word "swear" and substituting the phrase "under the pains and penalties of perjury" for the phrase "so help you God."
Other stastes have similar ruless
If asked by an official: "Do you solemnly swear that ...?" one may simply reply "I so affirm". This has the same legal effect as "I do" while not making an oath in a religious sense.