Solved

how do I use the Date object to print todays date of the format Use the Date object to print today’s date in this format Friday, December 17, 2010.

Posted on 2012-03-19
3
316 Views
Last Modified: 2012-04-07
Hi,

How do I use the Date object to print todays date of the format Use the Date object to print today’s date in this format Friday, December 17, 2010.

thanks
0
Comment
Question by:jecommera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Assisted Solution

by:moon_blue69
moon_blue69 earned 110 total points
ID: 37738388
Hi

I found the syntax in the following link

http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3

Format #5: day date month name year (something like, Wednesday 21st March 2001)
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 employ an array. This array would contain the Day names.

<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>
Note: For the sake of clarity, I've written the code for the arrays and the write() statement have been written on multiple lines. For usage, you would have to put this on a single line.

The Day name is accessed from the array using the value returned by getDay() as index.

The code above prints: :Monday 19th March 2012
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 110 total points
ID: 37738568
<!--
var date = new Date();

alert(date.toString("EEEE, MMMM dd, YYYY"))
-->

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
 
 <script type='text/javascript'>//<![CDATA[

Date.prototype.toStringOverridden = new Date().toString;
Date.prototype.toString = function(dateFormat) {
    var date = this;
    var result = this.toStringOverridden();
    if (arguments.length != 0) {
        result = dateFormat;
        var convert = {
            "EEEE": {
                part: new Date().getDay,
                replace: "$$1$1"
            },
            "EEE": {
                part: new Date().getDay,
                replace: "$$1$1"
            },
            "MMMM": {
                part: new Date().getMonth,
                replace: "$$2$1"
            },
            "MMM": {
                part: new Date().getMonth,
                replace: "$$2$1"
            },
            "MM": {
                part: getOrdinalMonth,
                replace: "$1"
            },
            "M": {
                part: getOrdinalMonth,
                replace: "$1"
            },
            "YYYY": {
                part: new Date().getFullYear,
                replace: "$1"
            },
            "dd": {
                part: new Date().getDate,
                replace: "$1"
            },
            "d": {
                part: new Date().getDate,
                replace: "$1"
            },
            "HH": {
                part: new Date().getHours,
                replace: "$1"
            },
            "H": {
                part: new Date().getHours,
                replace: "$1"
            },
            "mm": {
                part: new Date().getMinutes,
                replace: "$1"
            },
            "m": {
                part: new Date().getMinutes,
                replace: "$1"
            },
            "ss": {
                part: new Date().getSeconds,
                replace: "$1"
            },
            "s": {
                part: new Date().getSeconds,
                replace: "$1"
            },
            "SSS": {
                part: new Date().getMilliseconds,
                replace: "$1"
            },
            "S": {
                part: new Date().getMilliseconds,
                replace: "$1"
            },
            "Z": {
                part: getTimeZone,
                replace: "$Z$1"
            }
        };
        for (var pattern in convert) {
            result = result.replace(new RegExp(pattern, "g"), match);
        }
        result = result.replace(/\$1(\d{4})/g, dayOfWeekLong);
        result = result.replace(/\$1(\d{3})/g, dayOfWeekShort);
        result = result.replace(/\$2(\d{4})/g, monthLong);
        result = result.replace(/\$2(\d{3})/g, monthShort);
    }
    return result;

    function dayOfWeekLong() {
        return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][Number(arguments[1])];
    }

    function dayOfWeekShort() {
        return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][Number(arguments[1])];
    }

    function monthLong() {
        return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][Number(arguments[1])];
    }

    function monthShort() {
        return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][Number(arguments[1])];
    }

    function match(pattern) {
        return format(convert[pattern].part.call(date), pattern, convert[pattern].replace);
    }

    function format(value, pattern, replace) {
        var expression = new RegExp("^.*(\\d{" + pattern.length + "})$");
        return (pattern.length == 1) ? value : ("000" + value).replace(expression, replace);
    }

    function getOrdinalMonth() {
        return date.getMonth() + 1;
    }

    function getTimeZone() {
        return date.toStringOverridden().split(" ")[4];
    }
};
var date = new Date();

alert(date.toString("EEEE, MMMM dd, YYYY"))
//]]>  

</script>

</head>
<body>
</body>
</html>
0
 
LVL 6

Expert Comment

by:jjperezaguinaga
ID: 37739689
Greetings jecommera,

You should stop implementing utilities that has been already developed, tested and proved by other thousand developers.

Please use datejs, which is about 26kb not even minified. Date JS

Using that library your problem would be solved one lined:
//Friday, December 17, 2010.
Date.toString("dd, mmm d, yyyy");

Open in new window


Cheers,
-JJ
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question