Wrong time writing in .txt file

I'm trying to write soil moisture % to a micro Sd card with the date and time. I have run the following code for my RTC, which works. But when I load my program code, it is writing UTC time instead of EST.
Hardware used: Mega 2560, DS3231 AT24C32 IIC RTC, Micro SD Storage Board, 1.5inch RGB OLED display module, 128x128 pixels SSD1351, and Capacitive Soil Moisture Sensor

// Jack Christensen Aug 2012
#include <Wire.h>
#include <DS1307RTC.h>   // https://github.com/PaulStoffregen/DS1307RTC
#include <Timezone.h>    // https://github.com/JChristensen/Timezone

// US Eastern Time Zone (New York, Detroit)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240};    //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300};     //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);

// If TimeChangeRules are already stored in EEPROM, comment out the three
// lines above and uncomment the line below.
//Timezone myTZ(100);       //assumes rules stored at EEPROM address 100

TimeChangeRule *tcr;        //pointer to the time change rule, use to get TZ abbrev

void setup()
{
    Serial.begin(9600);
    setSyncProvider(RTC.get);   // the function to get the time from the RTC
    if(timeStatus()!= timeSet)
        Serial.println("Unable to sync with the RTC");
    else
        Serial.println("RTC has set the system time");
}

void loop()
{
    time_t utc = now();
    time_t local = myTZ.toLocal(utc, &tcr);
    Serial.println();
    printDateTime(utc, "UTC");
    printDateTime(local, tcr -> abbrev);
    delay(10000);
}

// format and print a time_t value, with a time zone appended.
void printDateTime(time_t t, const char *tz)
{
    char buf[32];
    char m[4];    // temporary storage for month string (DateStrings.cpp uses shared buffer)
    strcpy(m, monthShortStr(month(t)));
    sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
        hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
    Serial.println(buf);
}

My program code:

#include <DS3231.h>
RTClib myRTC;
#include <Timezone.h>

// SD - Version: 1.2.4
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>
#include <Wire.h>

// Screen dimensions
#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT 128 // Change this to 96 for 1.27" OLED.

// You can use any (4 or) 5 pins 
#define SCLK_PIN 29
#define MOSI_PIN 31
#define DC_PIN   25
#define CS_PIN   27
#define RST_PIN  23

// Color definitions
#define	BLACK           0x0000
#define	BLUE            0x001F
#define	RED             0xF800
#define	GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

// Option 1: use any pins but a little slower
Adafruit_SSD1351 display = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, CS_PIN, DC_PIN, MOSI_PIN, SCLK_PIN, RST_PIN);  

// Option 2: must use the hardware SPI pins 
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be 
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
//Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, CS_PIN, DC_PIN, RST_PIN);

float p = 3.1415926;

const int AirValue = 575;   //you need to replace this value with Value_1
const int WaterValue = 290;  //you need to replace this value with Value_2
int soilMoistureValue1 = 0;
int soilmoisturepercent1=0;
int soilMoistureValue2 = 0;
int soilmoisturepercent2=0;

File myFile;


void setup() {
  Wire.begin();

  Serial.begin(9600);
  display.begin();

  Serial.println("init");

  uint16_t time = millis();
  display.fillRect(0, 0, 128, 128, BLACK);
  time = millis() - time;
  
  Serial.println(time, DEC);
  delay(500);
  
 int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
 int moist1;
 int moist2;
 
 Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}

void loop() {
  soilMoistureValue1 = analogRead(A0);  //put Sensor insert into soil
Serial.println(soilMoistureValue1);
soilmoisturepercent1 = map(soilMoistureValue1, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent1 >= 100)
{
  Serial.println("100");
  
  display.setCursor(10,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("1");
  
  display.setCursor(2,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("100");
  
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent1 <=0)
{
  Serial.println("0");
  
  display.setCursor(10,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("1");
  
  display.setCursor(2,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("0");
 
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent1 >0 && soilmoisturepercent1 < 100)
{
  Serial.print(soilmoisturepercent1);
  Serial.println("%");
  
  display.setCursor(10,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("1");
  
  display.setCursor(2,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println(soilmoisturepercent1);

  delay(1);
  //display.fillScreen(BLACK);
}
  
    soilMoistureValue2 = analogRead(A1);  //put Sensor insert into soil
Serial.println(soilMoistureValue2);
soilmoisturepercent2 = map(soilMoistureValue2, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent2 >= 100)
{
  Serial.println("100");
  
  display.setCursor(35,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("2");
  
  display.setCursor(30,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("100");
  
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent2 <=0)
{
  Serial.println("0");
  
  display.setCursor(35,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("2");
  
  display.setCursor(30,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("0");
 
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent2 >0 && soilmoisturepercent2 < 100)
{
  Serial.print(soilmoisturepercent2);
  Serial.println("%");
  
  display.setCursor(35,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("2");
  
  display.setCursor(30,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println(soilmoisturepercent2);
  
  //delay(5000);
  //display.fillScreen(BLACK);
}
//DateTime now;
myFile = SD.open("datalog.txt", FILE_WRITE);
 DateTime now = myRTC.now();

    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print('/');
    myFile.print(now.year(), DEC);
    myFile.print(' ');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(' ');
    myFile.print(soilmoisturepercent1);
    myFile.print(' ');
    myFile.print(soilmoisturepercent2);
    myFile.println();

myFile.close();

delay(5000);
  display.fillScreen(BLACK);
}

I would also like it to write the time in 12 hour format, but not required and for the display to update every 5 minutes but write to the file every 30 minutes, again, not required

Your program code has no mention of setting the timezone so how to you expect it to happen? What have you tried?

Your code returns the current time as a DateTime object which you will have to convert to unix time (the library has this function) and then call the TZ code, just like the example

Also, on your soil measurements, you have if (>=100) else if (<=0) elseif ( >0 && < 100) which you don't need. If the value fails the if() and the elseif() you already know it is between 1 and 99. No need to test it again.

I figured out my problem, when adding the code from the example, I was leaving lines from the original RTC code in the program causing errors. Once I eliminated those lines it worked....kinda. My new problem is, I can't get the date and time to write to the file. It will display correctly when I monitor it through the Arduino Web Editor. But I can't figure out how to get it to write to the file.
My new code is:

#include <Wire.h>
#include <DS1307RTC.h>   // https://github.com/PaulStoffregen/DS1307RTC
#include <Timezone.h>    // https://github.com/JChristensen/Timezone

// SD - Version: 1.2.4
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>

// Screen dimensions
#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT 128

// You can use any (4 or) 5 pins 
#define SCLK_PIN 29
#define MOSI_PIN 31
#define DC_PIN   25
#define CS_PIN   27
#define RST_PIN  23

// Color definitions
#define	BLACK           0x0000
#define	BLUE            0x001F
#define	RED             0xF800
#define	GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

// Option 1: use any pins but a little slower
Adafruit_SSD1351 display = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, CS_PIN, DC_PIN, MOSI_PIN, SCLK_PIN, RST_PIN);  

// Option 2: must use the hardware SPI pins 
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be 
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
//Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, CS_PIN, DC_PIN, RST_PIN);

float p = 3.1415926;

const int AirValue = 575;   //you need to replace this value with Value_1
const int WaterValue = 290;  //you need to replace this value with Value_2
int soilMoistureValue1 = 0;
int soilmoisturepercent1=0;
int soilMoistureValue2 = 0;
int soilmoisturepercent2=0;

File myFile;

// US Eastern Time Zone (New York, Detroit)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240};    //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300};     //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);

TimeChangeRule *tcr;        //pointer to the time change rule, use to get TZ abbrev

void setup() {
  
  Serial.begin(9600);
  display.begin();

  Serial.println("init");

  uint16_t time = millis();
  display.fillRect(0, 0, 128, 128, BLACK);
  time = millis() - time;
  
  Serial.println(time, DEC);
  delay(500);
  
 int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
 int moist1;
 int moist2;
 
 Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");

{
    setSyncProvider(RTC.get);   // the function to get the time from the RTC
    if(timeStatus()!= timeSet)
        Serial.println("Unable to sync with the RTC");
    else
        Serial.println("RTC has set the system time");
}}

void loop() {
  soilMoistureValue1 = analogRead(A0);  //put Sensor insert into soil
Serial.println(soilMoistureValue1);
soilmoisturepercent1 = map(soilMoistureValue1, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent1 >= 100)
{
  Serial.println("100");
  
  display.setCursor(10,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("1");
  
  display.setCursor(2,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("100");
  
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent1 <=0)
{
  Serial.println("0");
  
  display.setCursor(10,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("1");
  
  display.setCursor(2,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("0");
 
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent1 >0 && soilmoisturepercent1 < 100)
{
  Serial.print(soilmoisturepercent1);
  Serial.println("%");
  
  display.setCursor(10,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("1");
  
  display.setCursor(2,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println(soilmoisturepercent1);

  delay(1);
  //display.fillScreen(BLACK);
}

    soilMoistureValue2 = analogRead(A1);  //put Sensor insert into soil
Serial.println(soilMoistureValue2);
soilmoisturepercent2 = map(soilMoistureValue2, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent2 >= 100)
{
  Serial.println("100");
  
  display.setCursor(35,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("2");
  
  display.setCursor(30,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("100");
  
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent2 <=0)
{
  Serial.println("0");
  
  display.setCursor(35,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("2");
  
  display.setCursor(30,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println("0");
 
  delay(1);
  //display.fillScreen(BLACK);
}
else if(soilmoisturepercent2 >0 && soilmoisturepercent2 < 100)
{
  Serial.print(soilmoisturepercent2);
  Serial.println("%");
  
  display.setCursor(35,0);  //oled display
  display.setTextSize(2);
  display.setTextColor(RED);
  display.println("2");
  
  display.setCursor(30,18);  //oled display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.println(soilmoisturepercent2);
  
  //delay(5000);
  //display.fillScreen(BLACK);
}
{
    time_t utc = now();
    time_t local = myTZ.toLocal(utc, &tcr);
    Serial.println();
      printDateTime(local, tcr -> abbrev);

myFile = SD.open("datalog.txt", FILE_WRITE);
    myFile.print(printDateTime(local, tcr -> abbrev));
    myFile.print(' ');
    myFile.print(soilmoisturepercent1);
    myFile.print(' ');
    myFile.print(soilmoisturepercent2);
    myFile.println();

myFile.close();

delay(5000);
  display.fillScreen(BLACK);
}}
// format and print a time_t value, with a time zone appended.
void printDateTime(time_t t, const char *tz)
{
    char buf[32];
    char m[4];    // temporary storage for month string (DateStrings.cpp uses shared buffer)
    strcpy(m, monthShortStr(month(t)));
    sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
        hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
    Serial.println(buf);
}

But I get the following error:

/tmp/255844529/moisture_sensor3/moisture_sensor3.ino: In function 'void loop()':

/tmp/255844529/moisture_sensor3/moisture_sensor3.ino:212:53: error: invalid use of void expression

myFile.print(printDateTime(local, tcr -> abbrev));

^

Error during build: exit status 1

If I get a good code on line 212, it writes random numbers or symbols.

I have not done much with eliminating the second 'else if' function, when I did, the display would show 0, but anything above 0 didn't show anything

PrintDateTime() does not return anything. It writes to Serial. Therefore you cannot write what it does not return to your SD card.
Does the DateTime library have a kind if sprintfDateTime() or snprintfDateTime() function?
That would allow you to retrieve the string ina char buffer[]. Then you can write that buffer to your SD.
Otherwise you may access the hour and minute the DateTime object...
Maybe something like...

int hour=localTime.hour();

I figured it out.

`myTZ.toLocal(utc, &tcr)

returns the correct date and time, so I used that to write to the sd card. Doing an over night check tonight.