DEV Community

Cover image for JavaScript Chapter 9 - Displaying the Date with JavaScript
Dzun N
Dzun N

Posted on

JavaScript Chapter 9 - Displaying the Date with JavaScript

In addition to PHP, with javascript we can also display the date. We can display dates in various formats. It can display days, minutes, seconds, months, years and more. Okay, let's just go into the discussion of this tutorial.

There is a special function provided in javascript to display the date. That is we can use the Date() function. By using this Date () function we can display the date. And to set the date format displayed, you can adjust it by giving/filling the date format as you wish in the Date() function parameter.
Friends need to pay attention. As we discussed in the previous javascript tutorial, javascript is case sensitive, which means writing javascript is very influential about the case size. So to make a date, my friend must write the date function correctly, namely the letter "D" must be a capital letter.

  • Incorrect Example> date()
  • True Example> Date() So if you write the Date() function using all lowercase letters, the results will not appear. So it must begin with a capital letter at the beginning, in accordance with the provisions of writing the date function.


Okay, it doesn't take long, I will give an example of using the Date() function in javascript


Pada contoh ini, saya memasukkan fungsi date ke dalam variabel tanggal dan kemudian menampilkannya

 var tanggal = new Date();

document.getElementById("hasil").innerHTML = tanggal;
Enter fullscreen mode Exit fullscreen mode

and the result



In conclusion To display the date in javascript we must use the Date() function and write Date() must begin with a capital D (Large) . When you enter the Date() function starting with lowercase letters, the function will not display an alias Error. But the example above is a very simple date creation, friend, you can give a css style so that the date looks more beautiful.

Top comments (0)