Solved

Format date time in html page

Posted on 2014-09-08
10
177 Views
Last Modified: 2014-09-27
Hi There,

The below code displayed current system date and time:

My question is how can I remove the time and GMT details from the date returned and display like

Monday 08 September 2014

<html>
 <head>
 <script type="text/javascript">
 document.getElementById('TD_ROW_ID').innerHTML = Date();
 </script>
 </head>
 <body>
 <table>
 <tr><td id="TD_ROW_ID"></td></tr>
 </table>
 </body>
 </html>

Open in new window

0
Comment
Question by:motolanix1
10 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 40309865
This should be what you are looking for:

var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();

Here is a full tutorial on how to work with dates:
http://www.w3schools.com/js/js_dates.asp
0
 

Accepted Solution

by:
motolanix1 earned 0 total points
ID: 40309908
It currently displays GMT+0100 (GMT Daylight Time at the end

How do I remove this?
0
 

Author Comment

by:motolanix1
ID: 40309919
Sorry I mean it displays Mon Sep 08 2014

How can it display Monday September 08 2014 or Monday Sep 08 2014
0
 
LVL 28

Assisted Solution

by:becraig
becraig earned 400 total points
ID: 40309967
Here you go:
<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>var date = new Date();
var options = {
    weekday: "long", year: "numeric", month: "long",
    day: "numeric", hour: "2-digit", minute: "2-digit"
};

document.write(date.toLocaleTimeString("en-us", options));
</script>

</body>
</html>

Open in new window

0
 

Author Comment

by:motolanix1
ID: 40309979
where is the id "demo" referenced in the code?

I don't see it?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:becraig
ID: 40309988
It's not referenced anywhere I should have stripped it out.

The only parts you need are:

<script>
var options = {
    weekday: "long", year: "numeric", month: "long",
    day: "numeric"
};
var d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleDateString("en-us", options);
</script>

Open in new window



Edited (Removed the time)
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 100 total points
ID: 40310012
test page : http://jsfiddle.net/b9qoosot/

window.onload = function() {
    var d = new Date();
    var days = [ "Sunday", "Monday" ]; // and so on
    var months = [ "January", "February", "March","April","May","June", "July","August","September", "October", "November","December"];
    // Monday 08 September 2014
    document.getElementById('TD_ROW_ID').innerHTML =  days[ d.getDay() ] + " " + d.getDate() + " " + months[ d.getMonth() ] + " " + d.getYear();
}

Open in new window

0
 
LVL 28

Assisted Solution

by:becraig
becraig earned 400 total points
ID: 40310026
<html>
 <head>
<script>
var options = {
    weekday: "long", year: "numeric", month: "long",
    day: "numeric"
};
var d = new Date();
document.getElementById("TD_ROW_ID").innerHTML = d.toLocaleDateString("en-us", options);
</script>
 </head>
 <body>
 <table>
 <tr><td id="TD_ROW_ID"></td></tr>
 </table>
 </body>
 </html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40311643
@becraig

toLocaleDateString("en-us", options);

is very cool, just note that the options are not supported by IE10 and lower and Safari.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Also be aware that w3schools is considered to be a very poor resource

Lastly note that you need to wrap your setting of innerHTML in a window.onload or move the script to after when the cell is defined
0
 

Author Closing Comment

by:motolanix1
ID: 40347437
Solution was simple and easy to understand.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

758 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

20 Experts available now in Live!

Get 1:1 Help Now