The command to fast has been given in detail in the Quran. We quote here the relevant verses:
Fast for a specified number of days, but if any one of you is ill, or on a journey, let him fast the same number of days later. For those who can fast only with extreme difficulty, there is a way to compensate — the feeding of a poor man. But he who does good of his own accord shall be well rewarded; but to fast is better for you, if you only knew.