Praise be to Allah.
It is permissible for the one who wants to observe a voluntary fast to form that intention during the day, unlike the obligatory fast, for which it is stipulated that the intention must be formed from the night before.
In order for a voluntary fast to be valid when the intention is formed during the day, it is stipulated that before forming that intention there should not have been anything that is contrary to fasting, such as eating, drinking and so on. If the person did anything before forming the intention that invalidates the fast, then it is not valid, and there is no difference of scholarly opinion concerning that.
End quote from al-Mulakhkhas al-Fiqhi (1/393)
Ibn Qudaamah al-Maqdisi said: Once this is established, one of the conditions is that the person should not have eaten before forming the intention, or have done anything that invalidates the fast. If he did any of these things, then fasting is not valid, and there is no difference of scholarly opinion concerning that as far as we know.
End quote from al-Mughni (3/115)
With regard to the hadeeths which speak of the Prophet (blessings and peace of Allah be upon him) instructing the people to complete the fast of ‘Ashoora’, regardless of whether or not they had eaten at the beginning of the day, the way in which this may be understood is that the fast of ‘Ashoora’ was obligatory upon them at that time.
With regard to the obligatory fast, it is obligatory for the one who finds out about it during the day to stop eating and drinking from the time he finds out about it.
Al-‘Ayni said concerning the fast of ‘Ashoora’: It was an obligatory fast.
End quote from ‘Umdat al-Qaari’ (10/304)
Al-Haafiz Ibn Hajar said: From many hadeeths on this topic, it may be understood that it was obligatory (to fast ‘Ashoora’), because the command to fast it is proven, then that was confirmed, and then confirmed again by the general call, and by the instruction to the one who had eaten to refrain from eating for the rest of the day, then by the instruction to mothers not to breastfeed their children during the day. Ibn Mas‘ood said: What is proven in Muslim is: When Ramadan was enjoined, ‘Ashoora’ was abandoned, but it should be noted that encouragement to fast it was not abandoned; rather that (encouragement) remains in effect, which indicates that what was abandoned was the obligation to fast it.
End quote from Fath al-Baari (4/247)
Imam an-Nawawi (may Allah have mercy on him) said:
“Whoever did not fast, let him fast, and whoever has eaten something, let him complete his fast until night comes.” According to another version: “Whoever started the day fasting, let him complete his fast, and whoever started his day not fasting, let him complete the remainder of his day.”
What these two reports mean is that whoever intended to fast should complete his fast, and whoever did not intend to fast but has not eaten anything, or he has eaten something, let him refrain from eating for the remainder of that day, out of respect for that day. This is like the situation where a person starts the day not fasting when there is some doubt about the day, then it is confirmed that it is indeed the first day of Ramadan. He must refrain from eating and so on for the remainder of that day, out of respect for that day.
End quote from Sharh Saheeh Muslim (8/13)
Al-Baaji said: This is like the case of the one who finds out that the day is Ramadan after part of the day has passed; he has to refrain from eating regardless of whether he has already eaten or not.
End quote from al-Muntaqa Sharh al-Muwatta’ (2/58)
But after the fast of Ramadan was enjoined, and ‘Ashoora’ became mustahabb (encouraged), this ruling no longer applied to it; rather it came under the same ruling as all other optional fasts: it is permissible to fast on that day with an intention formed on the day itself, on condition that one has not committed, prior to that, any action that invalidates the fast.
Shaykh Ibn ‘Uthaymeen was asked about a person who did not realise that it was the day of ‘Ashoora’ until partway through the day; is it valid for him to refrain from eating and drinking for the remainder of that day, noting that he had eaten at the beginning of the day?
He (may Allah have mercy on him) answered:
If he refrains from eating and drinking for the remainder of that day, his fast is not valid, because he ate at the beginning of the day. A voluntary fast is only valid from partway through the day for the one who has not consumed anything that breaks the fast from the beginning of the day. But if he has consumed anything that breaks the fast at the beginning of the day, then his intention to fast by refraining from eating and drinking for the remainder of the day is not valid. Based on that, it will not benefit him to refrain from eating and drinking so long as he has already eaten or drunk or done any of the things that break the fast at the beginning of the day.
End quote from Fataawa Noor ‘ala ad-Darb (11/2)
And Allah knows best.