No, the 21-day habit rule is pseudo-scientific.
First, it can take much longer:
These researchers asked 96 volunteers to form a daily habit, over a 12 week period, and record their progress.
The majority (82) of participants provided sufficient data for analysis, and increases in automaticity (calculated with a sub-set of SRHI items) were examined over the study period. Nonlinear regressions fitted an asymptotic curve to each individual's automaticity scores over the 84 days. The model fitted for 62 individuals, of whom 39 showed a good fit. Performing the behaviour more consistently was associated with better model fit. The time it took participants to reach 95% of their asymptote of automaticity ranged from 18 to 254 days; indicating considerable variation in how long it takes people to reach their limit of automaticity and highlighting that it can take a very long time. Missing one opportunity to perform the behaviour did not materially affect the habit formation process.
The source of the myth has been traced by Guardian columnist Oliver Burkeman who writes:
We probably owe this particular example of pop-psychology wisdom to Maxwell Maltz, the plastic surgeon who wrote the 60s bestseller Psycho-Cybernetics. He claimed to have observed that amputees took an average of only 21 days to adjust to the loss of a limb. Therefore, he reasoned – deploying the copper-bottomed logic we've come to expect from self-help – the same must be true of all big changes. And therefore it must take 21 days to change a habit, maybe, perhaps!