BACKGROUND Diabetes increases the risk of hepatocellular carcinoma (HCC), however, the time-relationship between hepatitis B virus and diabetes for the development of HCC remains unclear. AIM To explore the risk of HCC in chronic hepatitis B patients with newly diagnosed diabetes. METHODS We conducted a nationwide cohort study by using Taiwanese National Health Insurance Research Database, which covers over 99% of entire population. Among randomly sampled one million enrollees, 14 523 chronic hepatitis B patients were diagnosed in years 1997-2009. We defined new onset diabetes as patients who were given the diagnosis in the years 1999-2009, but not in 1997-1998. The cohorts of chronic hepatitis B with new onset diabetes (n = 2099) and 1:1 ratio age-, gender- and inception point (onset date of diabetes)- matched nondiabetes (n = 2080) were followed up from the inception point until development of HCC, withdrawal from insurance or December 2009. RESULTS After adjustment for competing mortality, patients with new onset diabetes had a significantly higher cumulative incidence of HCC [relative risk = 1.628, 95% confidence interval (CI) = 1.114-2.378, modified log-rank test, P = 0.012] as compared to nondiabetes patients. After adjustment for age, gender, hyperlipidaemia, chronic hepatitis B treatment, statins therapy, cirrhosis, comorbidity index and obesity, diabetes was still an independent predictor for HCC (hazard ratio = 1.798, 95% CI = 1.194-2.707, P = 0.005). CONCLUSION Chronic hepatitis B patients with newly diagnosed diabetes have an increased risk of hepatocellular carcinoma over time.