Here is an abstraction of the procedure, very useful if you eventually change your mind about the numbering.
\documentclass{article}
\usepackage{amsthm}
\usepackage{xparse,chngcntr}
\ExplSyntaxOn
\NewDocumentCommand{\newtheorembysubsection}{mom}
{
\IfNoValueTF{#2}
{
\newtheorem{#1}{#3}[subsection]
\cs_set:cpn { the#1 } { \thesection. \deepesh_subsection: \arabic{#1} }
\counterwithin*{#1}{section}
}
{
\newtheorem{#1}[#2]{#3}
}
}
\cs_new:Nn \deepesh_subsection:
{
\int_compare:nT { \value{subsection}>0 } { \arabic{subsection}. }
}
\ExplSyntaxOff
\newtheorembysubsection{theorem}{Theorem}
\theoremstyle{definition}
\newtheorembysubsection{definition}[theorem]{Definition} % could be \newtheorem
\begin{document}
\section{Test section}
\begin{theorem}
A theorem, numbered 1.1
\end{theorem}
\begin{definition}
A definition, numbered 1.2
\end{definition}
\subsection{Test subsection}
\begin{definition}
A definition, numbered 1.1.1
\end{definition}
\begin{theorem}
A theorem, numbered 1.1.2
\end{theorem}
\section{Test second section}
\begin{theorem}
A theorem, numbered 2.1
\end{theorem}
\subsection{Test subsection}
\begin{theorem}
A theorem, numbered 2.1.1
\end{theorem}
\end{document}
In case you decide to number theorems by section (as it should be, I find this scheme quite complex for the reader), you just change \newtheorembysubsection
into \newtheorem
, possibly adding a trailing [section]
if needed.

\usepackage{chngcntr}
and\counterwithin{theorem}{section}
in the relevant section should do, assuming that the counter is namedtheorem
.