1,360. Fasting Part of a Day
Hilchos Taaniyos 1:12 One who has a disturbing dream must fast the next day. This is to motivate him to improve his behavior, examine his deeds and turn to God in teshuvah (repentance). For this, one may fast even on Shabbos, reciting “Aneinu” in each Amida prayer. This is true even though one did not commit to the fast on the previous day. When a person has occasion to fast on Shabbos, he must also fast on another day as rectification for canceling out his enjoyment of Shabbos. Hilchos Taaniyos 1:13
A person may fast for just a few hours, simply not eating for the rest of the day. For example, if a person was involved with his business affairs and went without eating until noon, or until 3:00 pm, he may choose to fast for the rest of the day, reciting Anienu in Shemoneh Esrei. This works because he accepted the fast before the time of the fast. Similarly, if one ate or drank and then started to fast for the whole day, it is considered a fast of several hours.