The Bible does not specifically state how long Joseph was imprisoned in Egypt, although a general timeline can be deduced based on the information given in Scripture. After being sold by his brothers to Ishmaelites and taken to Egypt, Joseph was bought by Potiphar, the captain of the guard, and put in his service (Genesis 37:28; 39:1). An unspecified amount of time elapsed, and Joseph was put in charge of everything in Potiphar’s house (Genesis 39:2–6). During this time, Potiphar’s wife tried to seduce Joseph; when he refused, she accused him of making unwanted sexual advances, and Potiphar had Joseph thrown into prison (Genesis 39:7–20).
Joseph was in prison long enough for several things to happen: he gained favor in the prison warden’s eyes, he was put in charge of the prison (Genesis 39:20–23), and he met the pharaoh’s cupbearer and baker and interpreted their dreams (Genesis 40). Three days after Joseph gave his interpretations, the cupbearer was reinstated and the baker was executed. After that, two full years passed, and Joseph was still in prison (Genesis 40:23—41:1).
Scripture says that Joseph was 17 years old when his brothers sold him to the Ishmaelites (Genesis 37:2). By the time he was released from prison and appointed as head over Egypt, Joseph was 30 years of age (Genesis 41:46). Therefore, thirteen years passed from the time Joseph was sold by his brothers to the time he left prison. Some of that time, Joseph was in Potiphar’s service before he was wrongly accused by Potiphar’s wife; the rest of the time, he was in prison.
Some Jewish commentators have written that Joseph was imprisoned for twelve years, being only one year in Potiphar’s service. These commentators point to the apocryphal Book of Jasher as evidence: “And Potiphar heard their words, and he placed him in the prison house, the place where the king’s prisoners are confined, and Joseph was in the house of confinement twelve years” (Book of Jasher 44:76). It’s possible that Joseph was in prison for twelve years, but it’s also possible that he was in prison for much less time. There is just not enough biblical evidence to be conclusive.
God used the trials Joseph endured to bring about something good: “the saving of many lives,” including those of his brothers who had hated him so much (Genesis 50:20). We can learn a lot from the patient endurance of Joseph during the hardships he experienced in Egypt and trust that God is using all situations for His and our good (see Romans 8:28).