DS1307 not keeping time

I use Arduino mega with Ds1307. I have the following problem.
Time works only as long as power is connected to the plate.
When you switch off, and after a while turn back the clock is not more accurate.

Where is the problem???

This is my code:

// Date and time functions using just software, based on millis() & timer

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

RTC_Millis RTC;

void setup () {
Serial.begin(57600);

RTC.begin(DateTime(DATE, TIME));
lcd.begin(20, 4);

}

void loop () {
DateTime now = RTC.now();

Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print( " " );

lcd.setCursor(6, 1);
lcd.print(now.second(), DEC);
lcd.print( " " );

lcd.setCursor(3, 1);
lcd.print(now.minute(), DEC);
lcd.print( " " );

lcd.setCursor(2, 1);
lcd.print(":");

lcd.setCursor(5, 1);
lcd.print(":");

Serial.println();
delay(500);
}

do you have the backup battery installed?
is it full?

How far out is it?

I don't know which library you are using , but RTC.begin(DateTime(DATE, TIME)); suggests that the clock is being reset every time you power on the Arduino. Additionally, this sets the date and time to that of the computer AT THE TIME OF COMPILATION, which will always be several seconds behind the time at which the sketch starts running and displays the time.

What I used to do when I needed to set a clock was enter the values of the next whole minute or so, and upload the sketch. Then, just over one second before time, power up the Arduino. Then load it with another sketch which doesn't set the date and time.

@robtillaart
I installed the battery.
Battery is full

@dannable
which Library you use that time is not updated every time

I don't use any these days, I read and write directly!

I used to use the JeeLabs version http://news.jeelabs.org/code/ and that contains the lines:

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));

Without any power the DS1307 resets itself and also clears a flag which means that it is not running. Once you set the date and time that flag is then set - the condition above will only set the date and time once until you reset the chip again.

Another factor to consider is that these chips use a crystal intended for use in wrist watches and they need to be kept at a fairly even temperature, round about skin temperature, to maintain accuracy.