Found a way to do this.
For During the day, I make the background task run every 15 minutes and it check if the current time is between 9am and 5pm, if yes, it syncs else it exits and waits.
For At Night, I make the background task run every 15 minutes and it check if the current time is between 5pm and 9am, if yes, it syncs else it exits and waits.
Once the sync is done, i store the last synced time in a storage variable and every minutes even the last sync time is checked to see if its greater than 24 years for a subsequent sync.