Solved

Format date time in html page

Posted on 2014-09-08
10
180 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 29

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 29

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
 
LVL 29

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 29

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 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