Bereshit / Genesis 41:1 · Drash
וַיְהִי מִקֵּץ שְׁנָתַיִם יָמִים וּפַרְעֹה חֹלֵם וְהִנֵּה עֹמֵד עַל־הַיְאֹר׃
And it was at the end of two full years: Pharaoh was dreaming, and behold, he was standing by the Nile.
Drash דְּרָשׁ
He set an end to darkness
The midrash opens the chapter with a verse from Iyov: "He set an end to darkness." Every darkness has its term. Yosef's imprisonment had one too, measured down to the two extra years.
Bereshit Rabbah: God set the world a time, how many years it would be in darkness, and set Yosef a time, how many years he would sit in prison. When the end came, Pharaoh dreamed. Tanchuma: Yosef was due ten years, but he twice relied on the cupbearer, "remember me" and "mention me", and was given two more years.
How Bereshit Rabbah and Midrash Tanchuma (Buber) explain it
Bereshit Rabbah And it was at the end of two years
"He set an end to darkness" (Iyov / Job 28:3): He gave the world a time, how many years it would be in darkness. And why "He set an end to darkness"? Because as long as the evil inclination is in the world, there is gloom and the shadow of death in the world, as it is written: "the stone of gloom and the shadow of death" (there). When the evil inclination is uprooted from the world, there will be no gloom and no shadow of death in the world. Another explanation: "He set an end to darkness" — He gave Yosef a time, how many years he would be in darkness, in prison. When the end came, Pharaoh dreamed a dream: "and it was at the end of two years".
Midrash Tanchuma (Buber)
Another explanation. "He set an end to darkness": all the sufferings that come upon a person have an end, as it is said: "and every limit He searches out" (Iyov / Job 28:3). This speaks of Yosef. When he was put in prison he was there ten years, and in the eleventh he was due to go out. That is "He set an end to darkness". And what is "every limit He searches out"? When his time to go out came, the Holy One, blessed is He, sat and searched and saw that he needed to be in prison two more years, because he had relied on the chief cupbearer and said to him twice: "but remember me" and "and mention me to Pharaoh" (Bereshit / Genesis 40:14). The Holy One, blessed is He, said to him: you relied on him with two rememberings, by your life, you will serve two more years. As it is said: "and it was at the end of two years".
Takeaway
The midrash plays on the word קֵץ: "at the end" (מִקֵּץ) in the Torah and "He set an end to darkness" (קֵץ שָׂם לַחֹשֶׁךְ) in Iyov.
Twelve years in confinement: Yosef was seventeen when he was sold (37:2) and thirty when he stood before Pharaoh (41:46). Part of those years he served in Potiphar's house.
The midrash reads Iyov's "stone of gloom" as the evil inclination. The prophet Yechezkel also speaks of "a heart of stone" (Yechezkel / Ezekiel 36:26).