This works fine except it sets the RTC to the time at which the program was complied, not when it was uploaded so the time is always about a minute out.
You could change the date/time setting of your PC for time compensation before doing so.
But: When doing so, the time will always be set to the compile time - at the time the program executes. So if you upload such a program, and later do a RESET (i.e. by opening the serial monitor or a power-on), the program will start and set the RTC to date/time when it was compiled. Pretty useless, isn't it?
Is there anyway to compile first and then upload later if i set the time manually to say 16:00 and press upload at exactly 16:00 so i can finally get the current time on my RTC?
In normal cases you would provide a "set time" function in your software which allows setting time (and perhaps date). In my programs I usually use "Serial" for time setting. So while the firmware is running, I can open the serial monitor, enter the date and time as I want it, and as soon as I hit the RETURN key on my PC to send the string, containing command and new setting, the RTC time will be set to the date and time entered in the Serial monitor.