Solved

Javascript document.write problem with date()

Posted on 2008-06-11
5
444 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
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
russhuntington earned 500 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

856 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