首页 > 解决方案 > 如何将表单提交的日期/时间推送到 Firebase 数据库

问题描述

我正在尝试将表单提交的日期和时间与用户输入的电子邮件一起推送。电子邮件通过但日期/时间没有。

到目前为止我已经尝试过了......

//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,
     });
}

我只想让日期像在 console.log 中一样显示。例如

Thu Aug 29 2019 22:18:47 GMT+0530 (India Standard Time)```

标签: javascripthtmlfirebase

解决方案


您只需要将日期显式转换为字符串

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

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

推荐阅读