The section 21 notice period depends on the type of tenancy.
For fixed-term tenancies
If the tenancy is a fixed-term tenancy, you can only serve the notice during the fixed-term if there is a clause in the tenancy agreement allowing you to end the tenancy early (known as a 'break clause') or if the notice does not expire until after the fixed-term has ended.
If using a break clause, the notice must be sent 2 months prior to the break clause date (eg if there is a 6-month break clause, the notice can only be served at month 4).
For periodic tenancies
If the tenancy is a periodic tenancy, you can serve notice any time as long as the notice is for the correct length of time and contains any other information which may be required by law.
Where a landlord is seeking to evict a tenant who has always been on a periodic (or 'rolling') tenancy (ie a tenant who was never on a fixed-term tenancy), more notice may need to be given. This is because the notice needs to expire at the end of a rental period (ie the notice must expire on the last day of the rental period).
For example, take a periodic tenancy that rolls from month to month (ie rent being paid monthly on the first of the month). A landlord serves a section 21 notice partway through the month, on the 15 March. The eviction notice would have to provide 2 months' notice (ie 15 May) but would not be able to end until the last day of the rental period (ie the end of the rental month, here 31 May).