Spoiler alert: Never.
If you want to preserve your marriage, you must respect your spouse and trust that s/he wants a truthful answer to anything s/he asks. The truth can be hurtful, but it is impossible for your marriage to grow and thrive in the midst of a lie. You will live in fear that your spouse will discover the truth, and your spouse will be working under a false assumption, so any work you do to improve or even maintain the marriage will be done on a marriage that doesn't actually exist. Later, you will have to try to convince your spouse that, even though there was a lie between you, all that other stuff that you said (I love you, we're happy together, you're the only one for me) was true.
When you have to tell your spouse that you lied, you make him/her feel foolish, because you have been operating with a full set of facts and he/she has not had all the facts. Your spouse has been fooled by you into believing something that you knew wasn't true, and this makes him/her feel gullible.
Once the truth comes out (and yes, it will), the amount of trust lost in the relationship is proportional to the amount of time the secret has been kept. As hard as it is to come clean now, it will be much harder later!
There will never be "the right time" to tell someone something they don't want to hear. It's best to avoid late at night, but other than that, the best time to tell your spouse the truth is right now.
This should be distinguished from saying something hurtful to your spouse in order to cause pain, and then using the excuse that "it's just the truth." An example of this would be the classic "does this dress make me look fat?" You could say, "it's not your most flattering look", or "I really love the way you look in that other one." You do not need to say, "Yes, that dress makes you look terrible." You must not lie, but you don't need to say the first thing that comes into your head, either. If your spouse continues to ask for a direct answer, you may have to answer yes, but there are ways to cushion the truth in compliments. "I don't know if that's your color/style. Maybe we could go shopping this weekend for something you'd love. You deserve something new!"
The Gottman Institute research has found that for every one negative interaction between a couple, the couple must have five positive interactions in order to maintain a happy relationship. 5:1 seems like a pretty high ratio, particularly when a couple is going through a rough patch. A positive interaction can be a simple compliment and a "thank you" response. So if you are having to give your spouse a piece of bad news, keep this research in mind.
Lisitsa, E. (2017, March 13). The Positive Perspective: More on the 5:1 Ratio. Retrieved September 22, 2017, from https://www.gottman.com/blog/the-positive-perspective-more-on-the-51-ratio/