Background and Objectives: Migraines are one of the most common types of primary headaches in neurology. Many studies to date have investigated cognitive impairment in migraineurs, but the results are inconsistent. This study aimed to investigate the cognitive function of migraineurs and explore the influencing factors. Material and Methods: A total of 117 patients with primary headaches (87 with migraine and 30 with tension-type headache (TTH)) and 30 healthy controls were enrolled. General information and data on headache clinical characteristics, and assessments of headache-related disability, psychological symptoms, and cognitive function were collected for statistical analysis. Results: The Montreal Cognitive Assessment (MoCA) total score and the scores of visuospatial and executive functions, language, and delayed recall in the migraine and TTH groups were significantly lower than those in the healthy control group (all p < 0.05). The MoCA total score did not correlate with Headache impact test-6, Migraine Disability Assessment Questionnaire, Patient Health Questionnaire-9, or Generalized Anxiety Disorder Questionnaire-7 scores in migraineurs (all p > 0.0125). The multiple linear regression analysis showed that age and duration of attack had a major influence on the overall and various fields of cognition in migraineurs. Conclusion: The study confirmed the impairment of cognitive function in patients with migraine and TTH, and found that the duration of attack had an effect on cognitive function in migraineurs.