Background: Stroke is associated with an increased risk of dementia, but the temporal dynamics of dementia risk before and after stroke diagnosis remain uncertain. This study aimed to examine the risk of dementia from 10 years before through 30 years after stroke diagnosis, compared with non-stroke individuals. Methods: We performed a case-control study using the UK Biobank data. We identified all participants diagnosed with stroke who had disease occurrence data available in the database. Controls were matched 3:1 for year of birth, sex, and education level. Conditional logistic regression was applied to estimate odds ratios (ORs) for incident dementia across different time windows before and after stroke diagnosis. Results: This study included 24,056 individuals with stroke and 74,136 matched controls. The risk of dementia was higher in individuals with stroke compared to non-stroke controls in each time window before stroke diagnosis, with ORs ranging from 1.43 (95% CI: 1.02-2.01, P=0.040) in 5-10 years before diagnosis to 5.11 (95% CI: 4.06-6.41, P<0.001) in one year immediately before stroke. Within the first year after stroke diagnosis, the risk of incident dementia was the highest (OR: 6.39, 95% CI: 5.20-7.87, P<0.001). Similar results have been observed across sexes and different age groups. Conclusions: Participants who developed stroke had a higher risk of dementia beginning a decade before stroke onset, with risk peaking in the year around the diagnosis of stroke. These findings suggest the importance of prevention strategies at much earlier stages in individuals who are at risk of developing stroke and dementia.