A charged harmonic oscillator in a magnetic field, Landau problems, and an oscillator in a noncommutative space share the same mathematical structure in their Hamiltonians. We have considered a two-dimensional anisotropic harmonic oscillator with arbitrarily time-dependent parameters (effective mass and frequencies), placed in an arbitrarily time-dependent magnetic field. A class of quadratic invariant operators (in the sense of Lewis and Riesenfeld) have been constructed. The invariant operators (Î) have been reduced to a simplified representative form by a linear canonical transformation [the group Sp(4,R)]. An orthonormal basis of the Hilbert space consisting of the eigenvectors of Î is obtained. In order to obtain the solutions of the time-dependent Schrödinger equation corresponding to the system, both the geometric and dynamical phase-factors are constructed. A generalized Peres–Horodecki separability criterion (Simon’s criterion) for the ground state corresponding to our system has been demonstrated.