How to format date value in JavaScript?


To obtain dates in JavaScript, use either the new Date() or Date() function Object() { [native code] } (either current date or a specific date).

While the Date() function Object()produces a string containing the current date and time, the new Date() function Object()creates a new Date object. Let’s dive into the article for getting better understanding on how to format date value in JavaScript

How to Format Dates in JavaScript?

Date formatting is dependent on you and your requirements. In some nations, the year (06/22/2022) comes first, then the month. In other cases, the day precedes the month, the year (22.06.2022), and a whole lot more.

No matter the format, you need to dissect the date object value to retrieve the details you need for your web page.

For getting more idea about formatting date value in JavaScript, let’s look into the following examples.

Example

Consider the following example, here we are running the script to display the date format on the webpage.

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         var tutorial = {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
            weekday: 'long'
         };
         var local = new Date();
         document.getElementById("tutorial").innerHTML = (local.toLocaleDateString("en-US", tutorial));
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of day, month, and year formats on the webpage.

Example

Execute the below script and check how it work in formatting date.

<!DOCTYPE html>
<html>
   <body>
      <div id="tutorial"></div>
      <script>
         function dateformat() {
            var y = new Date();
            var day = y.getDate();
            var x = y.toDateString().substr(4, 3);
            var year = y.getFullYear();
            document.querySelector("#tutorial").innerHTML = day + '-' + x + '-' + year;
         }
         window.onload = dateformat;
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the date on the webpage in the format date-month-year.

Using JavaScript map()

Every element of the array is called by a function using the map() function to generate a new array. For every element in an array, Map() calls a different method once. The method for empty elements is not executed by Map(). The initial array is unaltered by map().

Syntax

Following is the syntax for map()

array.map(function(currentValue, index, arr), thisValue)

Example

In the following example, we are running the script using map() to format the date value.

<!DOCTYPE html>
<html>
   <body>
      <script>
         function join(A, tutorials, point) {
            function format(x) {
               let f = new Intl.DateTimeFormat('en', x);
               return f.format(A);
            }
            return tutorials.map(format).join(point);
         }
         let tutorials = [{
            day: 'numeric'
         }, {
            month: 'short'
         }, {
            year: 'numeric'
         }];
         let point = join(new Date, tutorials, '-');
         document.write(point);
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of a date printed on the webpage in the format of date/month/year.

Updated on: 21-Apr-2023

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements