European Union members have agreed to postpone Brexit for up to three months, stepping in with a decision less than 90 hours before Britain was due to crash out with no divorce deal.
The next deadline for departure will be 31 January next year - although the other 27 member states would allow an earlier date if the UK ratifies a withdrawal agreement before then.
"The EU27 has agreed that it will accept the UK's request for a Brexit flextension until 31 January 2020," said Donald Tusk, the president of the European Council, which represents member states.
"The decision is expected to be formalised through a written procedure," he said, after ambassadors met in Brussels to approve the extension.
This afternoon British Prime Minister Boris Johnson wrote to Mr Tusk confirming the UK's formal agreement to the extension.
In the letter, Mr Johnson said it is an "unwanted prolongation" that is damaging to British democracy and to the EU-UK relationship.
Mr Johnson said he accepted the decision but added he "would also urge EU Member States to make clear that a further extension after 31 January (2020) is not possible".
According to a copy of the agreement seen by AFP, if Mr Johnson convinces the UK parliament to approve an amicable divorce accord before next year, Brexit could be on 30 November or 31 December.
But in the meantime Britain must nominate a senior official to serve on the next European Commission and must agree that the Withdrawal Agreement it struck last month will not now be renegotiated, according to the EU text.
The EU27 has agreed that it will accept the UK's request for a #Brexit flextension until 31 January 2020. The decision is expected to be formalised through a written procedure.— Donald Tusk (@eucopresident) October 28, 2019
A spokesman for the Irish Government said it welcomed the decision of the EU27 to grant another extension to the UK.
In a statement, it said: "This averts the risk of a disruptive 'no deal' Brexit. The decision extends the deadline to the end of January, but allows for an earlier exit by the UK if ratification is completed quickly.
"We hope the extra time will be used to ensure that the Withdrawal Agreement agreed between UK and EU27 is ratified, enabling an orderly Brexit," it added.
Leaving the ambassador's gathering, EU negotiator Michel Barnier said it had been a "short and efficient and constructive meeting," adding: "I'm very happy that a decision has been taken."
A delay could have been agreed last week, but Paris was reluctant, concerned it would do nothing to boost the chances of Britain deciding how to handle the end of its five-decade relationship with the EU.
Mr Johnson had been pushing for a definitive break on 31 October after finally striking a withdrawal deal with fellow EU leaders at a 17 October summit.
But he has yet to persuade sceptical British MPs to ratify the accord, raising the spectre of a chaotic "no-deal" Brexit and severe economic disruption in the UK.
In the meantime, he is trying to break the logjam - and strengthen his tenuous grip on office - by demanding an early election to secure a parliamentary majority.
However, MPs in the House of Commons this evening voted against his plan for a pre-Christmas general election.
The motion won by 299 votes to 70, but under the Fixed-Term Parliament Act Mr Johnson needed a two-thirds majority - 434 MPs - to secure the election.