Solved

Javascript document.write problem with date()

Posted on 2008-06-11
5
435 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yeah, except that is so much more typing... :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now