skip to Main Content

I’m pretty new with HTML / JS
I have an HTML page like this

  <script src="https://www.gstatic.com/firebasejs/8.8.1/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.8.1/firebase-firestore.js"></script>
  
  <script>
    const name = document.getElementById("name").value;
    const email = document.getElementById("email").value;
    const category = document.getElementById("categories").value;
    const message = document.getElementById("message").value;
    var now = new DateTime( DateTimeZone.UTC ).toString();

But I am getting an error in the console:
Uncaught ReferenceError: DateTime is not defined

I am not sure how to import DateTime in my html file.

Any idea please?

2

Answers


  1. Instead of using DateTime, you should be using Date. Note that new Date().toUTCString() returns a string. I presume that you are using Firebase Firestore based on the above code. If so, this will be interpreted as a string field, not a timestamp field.

    const name = document.getElementById("name").value;
    const email = document.getElementById("email").value;
    const category = document.getElementById("categories").value;
    const message = document.getElementById("message").value;
    var now = new Date().toUTCString();
    
    Login or Signup to reply.
  2. Try to use this:

    <script src="https://www.gstatic.com/firebasejs/8.8.1/firebase-firestore.js"></script>
    
    <script>
      // Initialize Firebase
      firebase.initializeApp({
        // Your Firebase configuration here
      });
    
      const db = firebase.firestore();
    
      const name = document.getElementById("name").value;
      const email = document.getElementById("email").value;
      const category = document.getElementById("categories").value;
      const message = document.getElementById("message").value;
    
      // Get the current server timestamp from Firebase
      const timestamp = firebase.firestore.FieldValue.serverTimestamp();
    
      // Create an object with the data you want to store
      const data = {
        name: name,
        email: email,
        category: category,
        message: message,
        timestamp: timestamp
      };
    
      // Add the data to Firestore
      db.collection("yourCollection").add(data)
        .then(function(docRef) {
          console.log("Document added with ID: ", docRef.id);
        })
        .catch(function(error) {
          console.error("Error adding document: ", error);
        });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search