You can apply for an extension no more than four months before your current permit will expire. You must apply before your current permit expires.
If you apply too late
If you apply for too late, i.e. after your residence permit has expired, you will not be able to work while you wait for a decision. You also risk losing certain social benefits.
Moreover, if you apply too late, there is a risk your application will be rejected because you will have been in Sweden without a valid residence permit.