Need Help Writing Data and Time from RTC to SD Card

Hello! I am trying to write a script to print the time and date from an RTC and data from an analog pin to an SD card and I am having some trouble. Everything is wired correctly, I can read and write to the SD card and the RTC is giving me the correct time and date. The script I am posting was created by my colleague to read change on the A0 pin and I am trying to add the RTC and SD card scripts I was using to this existing script. Everything compiles fine but I am unable to write any data to the SD card and the serial monitor only prints 'initializing SD card'. What am I doing wrong here? I am a huge Arduino noob (bought my Uno two weeks ago now!) and I don't know the code very well. Any help you guys could provide would be most welcome; thanks Arduino Community!!

/*
  Gurly Flow meter logger
  Rockhound86 and Dr. M-H
*/

#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>

RTC_PCF8523 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

const float thresh = 10.0;       //value bellow which voltage is low due to switch closure 10~=0.05v
const float stradThresh = -1.0;  //measures how far sucessive readings are above and below thresh 

unsigned long lastDebounceTime = 0;  // the last time voltage change was detected
unsigned long debounceDelay = 20;    // the debounce time; increase if the output flickers

unsigned long initClosure = 0;   // initial closure time
unsigned long prevClosure = 0;   // second closure time
unsigned long closureInterval = 9999999; //time interval between switch closures, initiallize to 2:46:40hrs
float revpersec = 0.0;           //switch closures per second
float rpm = 0.0;                 //switch closures per min

int lastReading = 1000;
int switchState = 1000;
int crossingFlag = 0;
File datafile;
void setup() 
{
Serial.begin(57600);
 Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}
void loop() {
  int reading = analogRead(A0);
  if ((reading-thresh)*(lastReading-thresh) < stradThresh) {
    lastDebounceTime = millis(); // Reset the debouncing timer
    //Serial.println(77); //check lastDebounceTime set
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If the reading has been below (or above) thresh longer than 
    // debounceDelay, the voltage is not bouncing across thresh
     if ((reading-thresh)*(switchState-thresh) < stradThresh) {
      switchState = reading;
      Serial.println(888); //indicate threshold crossing
      datafile.print(888); //print to SD card

      if (switchState < thresh) { //if stable reading is below thresh the the switch is closed 
        initClosure=millis(); 
        closureInterval = initClosure-prevClosure;
        Serial.print(closureInterval);
        datafile.print(closureInterval);
        prevClosure=initClosure;
        Serial.write('  ');
        //rpm=60000.0/closureInterval;
        //Serial.print(rpm);
        Serial.write('  ');
        datafile.print(' ');
        Serial.println(reading);
        datafile.print(reading);
        }
      else {
        Serial.println(9999); //indicate switch opening
        datafile.print(9999);
      }
    }
  }
 
  datafile = SD.open("datafile.txt", FILE_WRITE);
DateTime now = rtc.now();  
if (! rtc.begin())
    Serial.println("Couldn't find RTC");
    while (1);

if (! rtc.initialized()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  lastReading = reading;
  datafile.close();
}

You seem to have made at least two mistakes:

You "datafile.print(reading);" BEFORE you open the file.

After you open the file you get the current date and time (DateTime now = rtc.now():wink: but you then don't use that value for anything, including not writing the date and time to the file.