user3397260
user3397260

Reputation: 271

Save the current Date and Time in firebaseDatabase

I am sending Data from a js.function to Firebase Database. The Data are consisting of two strings and 1 Data/time Variable the source code of the js function is the following:

$('#send_button').click(function(){
  rootRef.push({
    title:$('#title').val(),
    description:$('#description').val(),
    time: `${new Date().getTime()}.txt`;

    });

})

The function works perfect, my firebase database receives the content but it loos like this?

enter image description here

The time variable is storing just random numbers instead of the current date, is there any way to fix this?

Thanks in Regards

Upvotes: 0

Views: 1611

Answers (3)

Tushar Acharekar
Tushar Acharekar

Reputation: 900

new Date().getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT.

You can use toDateString() to convert date object in string

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript ISO Dates</h2>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = new Date().toDateString();
</script>

</body>
</html>

You can use toUTCString() to convert date object in string along with time

    <!DOCTYPE html>
    <html>
    <body>

    <h2>JavaScript ISO Dates</h2>

    <p id="demo"></p>

    <script>
    document.getElementById("demo").innerHTML = new Date().toUTCString();
    </script>

    </body>
    </html>

Upvotes: 1

L&#233;o R.
L&#233;o R.

Reputation: 2698

The GetTime method return the numeric value of a date ( so it's normal you have a number )

try this instead :

time: `${new Date().toISOString().split('T')[0]}.txt`;

Upvotes: 0

Alessio Libardi
Alessio Libardi

Reputation: 41

Those aren't random number, you used the 'getTime()' function which return the numeral time from the beginning of the world...obviously joking.

Read this: Date.prototype.getTime() - MDN

Also this: How do I get the current date in JavaScript?

Hopes it help!

Upvotes: 0

Related Questions