?
Solved

Javascript document.write problem with date()

Posted on 2008-06-11
5
Medium Priority
?
456 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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

807 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