how old was william shakespeare when he wrote hamlet
Between 1589 and 1613 at the age of 49.
Introduction

Shakespeare produced most of his known works between 1589 and 1613. His early plays were primarily comedies and histories and are regarded as some of the best work produced in these genres. Until about 1608, he wrote mainly tragedies, among them "Hamlet", "Romeo and Juliet", "Othello", "King Lear", and "Macbeth", all considered to be among the finest works in the English language. In the last phase of his life, he wrote tragicomedies (also known as romances) and collaborated with other playwrights. 
UNANSWERABLE UNANSWERABLE 44 years
William Shakespeare
Introduction
Until about 1608, he wrote mainly tragedies, among them "Hamlet", "Romeo and Juliet", "Othello", "King Lear", and "Macbeth", all considered to be among the finest works in the English language.
wiki:19635383
William Shakespeare [SEP] Introduction
Shakespeare was born and raised in Stratford-upon-Avon, Warwickshire. At the age of 18, he married Anne Hathaway, with whom he had three children: Susanna and twins Hamnet and Judith. Sometime between 1585 and 1592, he began a successful career in London as an actor, writer, and part-owner of a playing company called the Lord Chamberlain's Men, later known as the King's Men. At age 49 (around 1613), he appears to have retired to Stratford, where he died three years later. Few records of Shakespeare's private life survive; this has stimulated considerable speculation about such matters as his physical appearance, his sexuality, his religious beliefs, and whether the works attributed to him were written by others.