A plaintiff gets one free amendment of its original complaint, which ends either at the hearing on a demurrer to it or upon the filing of an answer. After that, any party may change a pleading up until the time of trial, and even afterwards with the court's approval. Whether the court gives its approval depends on many factors, including whether the party seeking amendment has been diligent once it learned the facts requiring an amendment and whether amending will prejudice the other party. Generally, the closer one gets to trial, the less likely the judge will allow an amendment.
Generally, when the judgment becomes final.
It is actually possible to file a post-trial motion to conform the pleading to the proof presented.
If you are trying to handle such a case without an attorney, this is generally a very bad idea.
Just before the end of trial in California, many lawyers say to the court: "We move to amend the complaint to conform to the proof". Standing is a totally different matter.
The above is general legal and business analysis. It is not "legal advice" but analysis, and different lawyers may analyse this matter differently, especially if there are additional facts not reflected in the question. I am not your attorney until retained by a written retainer agreement signed by both of us. I am only licensed in California. See also avvo.com terms and conditions item 9, incorporated as if it was reprinted here.