Do not replace the water heater without first checking out the pressure balance faucet in the shower. Pressure balance and temperature control shower faucets are notorious for reducing the hot water pressure as you are using the faucet. Temperature control shower faucets use a spring that adjusts the hot water temperature down when it senses a change in the water temperature. Unfortunately when the spring gets old, it doesn't function as it was intended and you lose hot water temperature.
When you experience a rapid loss of hot water, the problem is probably the dip tube. This tube is really a pipe that is is attached to the cold water inlet pipe (inside the water heater) and transports the cold water through the hot water to the bottom of the water heater where the burner can heat it up. It is designed to prevent the cold water from coming into the top of the water heater and running right over to the hot water outlet and out to the faucets. All water that is brought in through the cold pipes ends up on the bottom of the water heater and the hot water stays on the top. Once in a while the dip tube falls off inside the hot water heater and the cold water doesn't go to the bottom but stays on the top, so you get cold water out of the hot water faucet after only a few minutes. In order to stop your loss of hot water, you will need to pull the cold water inlet pipe and verify the condition of the dip tube.
Most people are comfortable with their hot water set at 120°F, which is also the new standard that manufacturers use when pre-setting your water heater temperature at the factory. If you have an older model, set the thermostat at medium. On a gas model, there is a dial on the front of the gas valve. On electric models, the thermostats (there may be two) are concealed behind the two panels on the side of the tank. NOTE: Turn off the electricity before removing the panels. There are exposed wires behind the panels containing HIGH VOLTAGE.
There are only a couple of things that will cause an electric hot water heater to raise its water temperature by itself. The first is a bad thermostat. A thermostat is designed to shut the temperature of the water off when it reaches the chosen temperature, but if the heat-sensing device goes bad, then the water temperature will continue to climb until the backup thermostat shuts off the power. Both thermostats need to be tested to see which one has failed. You will probably need a professional to pull it out and replace it. Unless you are very handy and know electrical safety, it is not recommended that you touch it. Electric water heaters typically have 240 volts going to the thermostat.
The other problem that could happen with an electric water heater is the tube surrounding the element, which separates the element from the water, may be damaged and the element may be heating the water directly. Generally you get hot water for a short period of time, then the element burns out. Sometimes you don't notice that it's gone because the other element continues to heat water. Every electric water heater has two elements, an upper element and a lower element. Both cycle to heat the water. An element is tested with an ohm meter; a small current is passed through the element to see if it is unbroken. If you get a reading from both poles of the element and it is within tolerance, then the element is still good. You have to test it with the power leads disconnected. Again, do not attempt this unless you know all of the safety procedures for 220 volts of power.
If the water heater is more than 10 years old, it is time to consider installing a new water heater. The time factor and parts for rebuilding the old water heater is the same as replacing it with a new one. You may be able to conserve more heat by installing a more energy-efficient model. Enough power may be conserved to eventually pay for the water heater. In addition, you may be able to easily convert to a tankless water heater that stores no hot water and is therefore even more energy efficient.
There are two possibilities for rapidly reducing water temperatures. First, the dip tube in your water heater may have broken off. This is a tube that forces incoming water to the bottom of the tank so that hot water will be drawn off of the top. When the dip tube breaks, cold water entering the tank mixes with the hot water and cools it down. This can occur in both gas and electric models.
Second, if your water heater is electric, the lower element that heats the water may not be operating properly, thus only the upper half of the tank will heat up. The cause of this problem could be a bad element or a malfunctioning thermostat. This type of problem should be evaluated by a qualified technician.