In 1960, a plastic surgeon by the name of Maxwell Maltz published a wildly popular book that spawned a spurious factoid – it takes just 21 days to change your ways and form a new habit.

That figure was based on Maltz's observations of the time it took his patients to adjust to their new faces. While this has little to do with changing behaviors, many of us still cling to the promise that in a few short weeks, we can make small but impactful changes to our daily lives.

Now scientists from Caltech, the University of Chicago, and the University of Pennsylvania have challenged that already iffy idea using data from more than 30,000 gym goers who worked out some 12 million times over a four-year period, and more than 3,000 hospital workers who collectively washed their hands 40 million times over nearly 100 shifts.

Using machine learning tools to analyze when people's behaviors became predictable – and therefore, habitual – the researchers found that (perhaps unsurprisingly) some habits take longer to form than others. Getting into the groove of working out takes on average around six months, for example.

"Contrary to the popular belief in a 'magic number' of days to develop a habit, we find that it typically takes months to form the habit of going to the gym but weeks to develop the habit of handwashing in the hospital," write the team of behavioral scientists, led by Colin Camerer at the California Institute of Technology (Caltech).

Trying to understand habits – how to make them and how to break them – is of perpetual interest to psychologists for obvious reasons. Habits, both good and bad, can have huge ramifications for our health and well-being: simple habits can ease the mental strain of making a bazillion itty-bitty decisions in a day; lives beset by addiction can quickly unravel.

Despite the attention, this new research is one of only a few studies that have looked at how quickly people form habits in real-world settings, outside the artificial realm of psychology labs.

One seminal 2009 study found that it took people roughly two months to establish a habit that was tied to a daily cue of, for example, eating breakfast. But there was huge variation amongst the 96 volunteers: it took anywhere from 18 to 254 days for people to feel like their newly formed habit had become automatic.

Those studies, however, relied on people filling out surveys to report their behaviors whereas this new study analyzed point-source data on gym visits and handwashing practices to see when actual, repeated behaviors became predictable.

Just as gym members had to swipe in on arrival, staff involved in a separate study monitoring handwashing in hospitals had to scan an ID card each time they washed their hands. The data included details that allowed the researchers to study certain variables such as the time of day or day of the week, to determine if it had any effect on an individual's behavior.

"With machine learning, we can observe hundreds of context variables that may be predictive of behavioral execution," explains behavioral scientist Anastasia Buyalskaya, who now works for a French marketing firm after completing her graduate studies at Caltech.

If more time had passed since they last hit the gym, gym goers were less likely to return, but the time of day had little bearing on people's habitual attendance. As with past research, it seems that keeping some flexibility in an exercise routine is important, but so too is consistency.

Two-thirds of gym-goers stuck to the same days of the week, with Mondays and Tuesdays being popular – and again, this echoes other studies suggesting motivation peaks around so-called fresh-start dates.

As for making their new exercise habit stick, that took between four and seven months, the modeling suggests, which is more than double what previous studies had found. On the flip side, it was only a matter of weeks before health workers began routinely washing their hands.

It goes to show that forming a new habit really depends on the person, but also on the behavior itself, how much time and effort it takes, and the cue that triggers it.

Some people might take that to the extreme, sleeping in their running clothes to remove any 'friction' to exercising in the morning as planned. But for most of us, with time and repetition, habits will slowly form – if we can find the right motivation.

The study has been published in PNAS.