Quarantine and isolation at extended length, although considered as highly effective countermeasures for the novel coronavirus which started at the end of 2019, can have great impact on individual's mental health, especially emotional state. The present research recruited 5,115 participants from the general public across 32 provinces and autonomous regions in China in an online survey study, about 20 days after the lockdown of the epicenter, to investigate the relationship between the length of the quarantine and negative affect, as well as the mediating roles of negative cognition, and the moderating roles of dispositional optimism, tolerance of uncertainty, social support, and healthy behavior. The results showed that: Worry and anticipation mediated the relationship between quarantine length and depression and anxiety; Dispositional optimism moderated the path coefficients of quarantine length to worry, worry to anxiety, and anticipation to depression; Tolerance of uncertainty moderated the path coefficient of worry to anxiety; Social support moderated the path coefficient of anticipation to anxiety. In conclusion, during quarantine, dispositional optimism, uncertainty tolerance, and social support can buffer the direct or indirect effects of quarantine length on depression and anxiety. These findings could have profound implications on the societal responses to COVID-19 and future pandemics.