The "day" comes from scriptures like the following:
Genesis 1:5 - "Yahweh called the light Day, and the darkness he called Night. So there was evening, and there was morning, one day."
Deuteronomy 16: "Neither should any of the meat which you slaughter in the evening on the first day stay all night until morning.
Nehemiah 13: 19 It came about that just as it grew dark at the gates of Jerusalem before the sabbath, I commanded that the doors should be shut and that they should not open them until after the sabbath. Then I stationed some of my servants at the gates so that no load would enter on the sabbath day.
2 Chronicles 2: 4 Here, I am about to build a house for the name of ADONAI my God, to dedicate it to him, and to burn before Him incense made of sweet spices; the house will also be for the continuing showbread and for the burnt offerings presented every morning and evening, on the shabbats, at every Rosh-Hodesh, and at the designated times of ADONAI our God. This is a perpetual regulation for Isra'el.
So a whole "day" is measured from dark - that is, sunset - to the next sunset. Therefore a full "day" is sunset, through all night, through all day, until the sun sets again. Therefore, the Sabbath begins when the sun sets on Friday, and it ends when the sun sets on Saturday. Of course, according to the Gregorian calendar, Saturday remains "Saturday" until midnight, because we must use modern civil time to determine our days which are measured from midnight to midnight....
There is another meaning of the word "day" as you can see in the scripture above: When it is light - that is, the sun is up - it is also called "day". So you must take care to determine what "day" is meant as you read the Bible. In Matthew 12:40, Yeshua is referring to "day" as in "daylight", when the sun is up. Therefore when He says "For just as Yonah was three days and three nights in the belly of the sea-monster, so will the Son of Man be three days and three nights in the depths of the earth", He means "3 daytimes, and 3 night-times". In other words, 3 full days.
One problem here is that the English word "day" means both a full 24 hour day, and it means the period in which the sun is up. Another problem is that any 24 hour period is considered a "day". To explain this, try to imagine that at sunset, you are starting your clock at zero. Then, the next evening, your clock will show 23 hours and about 56 minutes have passed when the sun sets again. Just at sunset, your clock reads zero again.
This is not any different than our civil world where the clock changes from 23:59 to 00:00 during the middle of the night. Society has simply chosen to determine the "day" as measured from midnight to midnight, whereas Yahweh said it is from sunset to sunset.