1

I am trying to push the date and time of Form Submission along with the email the user inputs. the email comes through but the date/time does not.

I have tried this so far ...

//Listen for form submit
document.getElementById('cta_form').addEventListener('submit', submitForm);


//Submit Form
function submitForm(e){
    e.preventDefault();

    //Get Valuse
    var email = getInputVal('email');

    var email_date = new Date();
    console.log(email_date);

    //save email
    saveEmail(email,date);


    //Show alert
    document.querySelector('.alert').style.display = 'block';

    //set alert timeout
    setTimeout(function(){
        document.querySelector('.alert').style.display = 'none';
    },3000);

    //Clear Form
    document.getElementById('cta_form').reset();
}

//function to id values
function getInputVal(id){
    return document.getElementById(id).value;
}

//Save emails to firebase
function saveEmail(email,email_date){
     var newEmailRef = emailsRef.push();

     newEmailRef.set({
         email: email,
         date: email_date,
     });
}

I just want the date to show up as it does in console.log. e.g

Thu Aug 29 2019 22:18:47 GMT+0530 (India Standard Time)```
Justin
  • 141
  • 3
  • 12
  • Possible duplicate of [Saving and retrieving date in Firebase](https://stackoverflow.com/questions/37976468/saving-and-retrieving-date-in-firebase) – Daniel Mai Aug 29 '19 at 16:56
  • https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp – Daniel Mai Aug 29 '19 at 16:58

2 Answers2

1

You just need to explicitly turn your date into a string

function saveEmail(email,email_date){
     var newEmailRef = emailsRef.push();

     newEmailRef.set({
         email: email,
         date: "" + email_date,
     });
}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

The console.log is interpreting the Date object for you. This is not how Date is 'printed' but how Console is helping you out. To properly extract a 'formatted date' from a Javascript Date object, you should do something like the following:

Date.toString()

If you would like to print a specific format, you need to call .getYear() etc. Read up here: https://javascript.info/date

Rack Sinchez
  • 111
  • 3