The Queen will carry out an official engagement on the day she becomes the longest reigning monarch in British history - opening the new Scottish Borders Railway.

On September 9, she will pass the record set by her great-great-grandmother Queen Victoria.

Buckingham Palace has calculated that Queen Victoria reigned for 23,226 days, 16 hours and 23 minutes, taking into account 63 years, 15 leap days and the precise timings of her accession and death.

On the historic day the Queen and Duke of Edinburgh will be joined by First Minister Nicola Sturgeon on a steam train travelling along the new route with celebrations planned for the start and end of the journey.

The £294 million railway, which opens to the public on Sunday September 6, will re-establish passenger services from Edinburgh through Midlothian to Tweedbank in the Borders for the first time in more than 40 years.

Keith Brown, the Scottish Government's cabinet secretary for infrastructure, said: "It is a great honour for us that Her Majesty has chosen to mark this milestone by leading the celebrations for the opening of the Borders Railway.

"The people of the Borders and Midlothian have waited almost half a century to see the return of their railway and it is fitting that these two such historic events coincide.

"This will be a double celebration and we are also ensuring that the people who have campaigned, worked hard and shown such patience throughout this process are able to play a full part in the celebrations, with a separate day dedicated just to them."

The day will begin with the Queen and Philip arriving at Waverley Station in Edinburgh to board the train drawn by the steam locomotive Union of South Africa.

With Ms Sturgeon they will travel the new route, stopping off at Newtongrange in Midlothian before carrying on to the final station on the new line, the Borders town of Tweedbank.

Mike Cantlay, chairman of VisitScotland, said: "Royal connections to Scotland are incredibly strong, and so there is no better place to be than our wonderful capital city, launching one of our most exciting new attractions - the Borders Railway.

"The new Borders Railway, which delves deep into Sir Walter Scott country, presents a huge opportunity for Scottish tourism."

He added: "The romanticism captured by the steam trains that will be travelling on the line for the first six weeks after opening will give the route added value, following in the tracks of the Jacobite steam train journey from Fort William to Mallaig which welcomes thousands of visitors every year."

Work started on the Borders Railway in 2013 with the last bridge installed in May this year and driving and conductor training beginning the following month. Seven new stations have been constructed along the 10-stop route.

It was built by Network Rail in partnership with local authorities from the area, Transport Scotland and the main contractor, construction group Bam Nuttall, with services run by ScotRail.