Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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.

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
jecommera
Asked:
jecommera
2 Solutions
 
moon_blue69Commented:
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
 
ProculopsisCommented:
<!--
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
 
jjperezaguinagaCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now