0

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

KENdi
  • 7,576
  • 2
  • 16
  • 31
user3397260
  • 241
  • 5
  • 17

3 Answers3

1

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>
Tushar Acharekar
  • 880
  • 7
  • 21
0

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!

0

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`;
Léo R.
  • 2,620
  • 1
  • 10
  • 22