Mental disorders are an underappreciated category of modifiable risk factors for dementia. Developing an evidence base about the link between mental disorders and dementia risk requires studies that use large, representative samples, consider the full range of psychiatric conditions, ascertain mental disorders from early life, use long follow-ups, and distinguish between Alzheimer disease and related dementias. To test whether mental disorders antedate dementia across 3 decades of observation. This population-based administrative register study of mental disorders and Alzheimer disease and related dementias included all individuals born in New Zealand between 1928 and 1967 who resided in the country for any time during the 30-year observation period between July 1988 and June 2018. Data were from the New Zealand Integrated Data Infrastructure, a collection of whole-of-population administrative data sources linked at the individual level. Data were analyzed from October 2020 to November 2021. Diagnoses of mental disorders were ascertained from public-hospital records. Diagnoses of dementia were ascertained from public-hospital records, mortality records, and pharmaceutical records. Of 1 711 386 included individuals, 866 301 (50.6%) were male, and individuals were aged 21 to 60 years at baseline. Relative to individuals without a mental disorder, those with a mental disorder were at increased risk of developing subsequent dementia (relative risk [RR], 4.24; 95% CI, 4.07-4.42; hazard ratio, 6.49; 95% CI, 6.25-6.73). Among individuals with dementia, those with a mental disorder developed dementia a mean of 5.60 years (95% CI, 5.31-5.90) earlier than those without a mental disorder. Associations held across sex and age and after accounting for preexisting chronic physical diseases and socioeconomic deprivation. Associations were present across different types of mental disorders and self-harm behavior (RRs ranged from 2.93 [95% CI, 2.66-3.21] for neurotic disorders to 6.20 [95% CI, 5.67-6.78] for psychotic disorders), and were evident for Alzheimer disease (RR, 2.76; 95% CI, 2.45-3.11) and all other dementias (RR, 5.85; 95% CI, 5.58-6.13). In this study, mental disorders were associated with the onset of dementia in the population. Ameliorating mental disorders in early life might also ameliorate neurodegenerative conditions and extend quality of life in old age.