?
Solved

Javascript document.write problem with date()

Posted on 2008-06-11
5
Medium Priority
?
451 Views
Last Modified: 2008-06-11
Hi guys hope u r well and can assist.

I have the following 1 liner javascript file.....

document.write(Date());

The output is the following..

Wed Jun 11 2008 17:56:28 GMT+1000 (AUS Eastern Standard Time)

Is there any way to modify the document.write(Date());
so that I only get the following?

Wednesday June 11, 2008 17:56:28

So, the
GMT+1000 (AUS Eastern Standard Time) - is removed.
The day is expanded to its full name
The month is also expanded to its full name.

Would really appreciate your help.
Thank you.
0
Comment
Question by:Simon336697
[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
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
russhuntington earned 2000 total points
ID: 21757871
There a few download-able formatting functions out there, but you could just write your own as you exactly what you want. Try this:
<script type="text/javascript">
 
var d = new Date();
formatDate(d)
 
function formatDate(date)
{
var days = new Array();
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday";
days[5] = "Friday";
days[6] = "Saturday";
 
var months = new Array();
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";
 
var dateString = days[date.getDay()] + " " + months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
document.write (dateString);
}
 
</script>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21757930
HI russhuntington,

Mate thank you so much for that..
That is exactly what I was looking for.
Really appreciate it.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21759173
FYI:

  The syntax used above is very verbose.  Here are more concise ways to initialize
the two arrays to the same values as shown above:

var days = 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split( ',' );

var months = 'January,February,March,April,May,June,July,August,September,October,November,December'.split( ',' );
0
 
LVL 2

Expert Comment

by:russhuntington
ID: 21760912
Good point, although I think would go for the following instead of the split method.

var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21761458
yeah, except that is so much more typing... :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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

770 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