Formatting-day-date-month-name-year

Wikitechy | 2621 Views | javascript | 04 Jun 2016

 

  • The getDay() method returns a number that specifies the day of the week. 
  • Sunday is represented by 0, Monday by 1 and so on. 
  • Here again we deploy an array. This array would contain the Day names.

Syntax :

var m_names = new Array();
var sup = "  " ;

Sample Code :

<html>
<head>
    <title> Formatting-date </title>
</head>
<body>
    <script type="text/javascript">
        var d_names = new Array("Sunday", "Monday", “Tuesday",  "Wednesday",
            "Thursday", "Friday", "Saturday");
        var m_names = new Array("January", "February", "March", "April",  "May", "June",
            "July", "August", "September", "October", "November", "December");
        var d = new Date();
        var curr_day = d.getDay();
        var curr_date = d.getDate();
        var sup = "";
        if(curr_date == 1 || curr_date == 21 ||curr_date ==31)
        {
            sup = "st";
        }
        else if (curr_date == 2 || curr_date == 22)
        {
            sup = "nd";
        }
        else if (curr_date == 3 || curr_date == 23)
        {
            sup = "rd";
        }
        else
        {
            sup = "th";
        }
        var curr_month = d.getMonth();
        var curr_year = d.getFullYear();
        document.write(d_names[curr_day] + " " + curr_date + "<SUP>" 
            + sup + "</SUP> " + m_names[curr_month] + " " + curr_year);
    </script>      
</body>
</html>

Code Explanation :

    Here the var d_names = new Array holds ("Sunday", "Monday", “Tuesday",  "Wednesday",  "Thursday", "Friday", "Saturday"); specify the getDay() function which gives us the day in a string form. To convert this value into the day name, we will deploy an array. The array would contain all the 7 days or weeks names.

  Here the var m_names = new Array holds ("January", "February", "March", "April", "May", "June", "July", "August", "September",  "October", "November", "December"); that specify the getMonth() function which gives us the month in a non-numeric form. To convert this value into the month name, we will deploy an array. The array would contain all the 12 month names.

    var d = new Date () specifies a variable d contains a new date object. We can now call the various methods of this date object.

    var curr_date = d.getDate()  return the current day of the month.

    var curr_day = d.getDay(); it return the current day.

    var sup = "" specifies to returns the e sup() method is used to display a string as superscript text.


    sup = "st"; specifies “st” is used with numbers ending in 1 such as 1st , 21st, 31st pronounced First, Twenty-first, Thirty-first.

  • We first initialize a variable sup that would store the superscript value. Using else if (curr_date == 2 || curr_date == 22) we check the value of the current date and accordingly we assign a value to sup (super script). (if condition is false it goes to else if condition).
    • sup = "nd"; specifies “nd” which  is used with numbers ending in 2 like 2nd, 22nd pronounced second, twenty-second.
  • We first initialize a variable sup that would store the superscript value. Using else if (curr_date == 3 || curr_date == 23) we check the value of the current date and accordingly we assign a value to sup(super script). (if condition is false it goes to else condition).
    • sup = "rd"; specifies “rd” is used with numbers ending in 3 e.g. 3rd ,23rd pronounced third , Twenty-third.
    • sup = "th"; specifies as an exception to the above rules, all the "teen" numbers ending with 11th , 12th .
    var curr_month = d.getMonth() specifies the current month.

   var curr_year = d.getFullYear() specifies the method returns the year (four digits for dates between year 1000 and 9999) of the specified date.

    document.write specifies the write() method writes JavaScript code to a document.(d_names[curr_day] + " " + curr_date + "<SUP>" + sup + "</SUP> " + m_names[curr_month] + " " + curr_year);it returns the current date with superscript text month name year (like: Monday 30th May 2016) in this format.

Sample Output :


    Here in this output we display Monday 30th May 2016 specifies the day or week datesup (date superscript text) month year.



Workshop

Bug Bounty
Webinar

Join our Community

Advertise
<