Solved

Date Time format javascript

Posted on 2013-01-28
6
485 Views
Last Modified: 2013-01-30
Hi,

I have the below JavaScript that renders the system date time in the format: "28 January 2013, 10:56 PM". How can I alter this script to output the format as: "28th January 2013, 22:56"

<td class="homepage-date" valign="top">
                                        <script type="text/javascript">
                                            var months=new Array(13);
                                            months[1]="January";
                                            months[2]="Febuary";
                                            months[3]="March";
                                            months[4]="April";
                                            months[5]="May";
                                            months[6]="June";
                                            months[7]="July";
                                            months[8]="August";
                                            months[9]="September";
                                            months[10]="October";
                                            months[11]="November";
                                            months[12]="December";
                                            var day=new Date();
                                            var lmonth=months[day.getMonth() + 1];
                                            var date=day.getDate();
                                            var year = day.getFullYear();
                                            document.write(date + " " + lmonth + " " + year + ",");
                                        </script>
                                    </td>
                                    <td class="homepage-time" valign="top">
                                        <script type="text/javascript">
                                            function getClockTime()
                                            {
                                                var now    = new Date();
                                                var hour   = now.getHours();
                                                var minute = now.getMinutes();
                                                var second = now.getSeconds();
                                                var ap = "<span class='company-homepagetime-ampm'>AM</span>";
                                                if (hour   > 11) { ap = "<span class='company-homepagetime-ampm'>PM</span>";}
                                                if (hour   > 12) { hour = hour - 12;      }
                                                if (hour   == 0) { hour = 12;             }
                                                if (minute < 10) { minute = "0" + minute; }
                                                if (second < 10) { second = "0" + second; }
                                                var timeString = hour +
                                                    ':' +
                                                    minute +
                                                    " " +
                                                    ap;
                                                return timeString;
                                            } // function getClockTime()
                                                var clockTime = getClockTime();
                                                document.write(clockTime);
                                        </script>
                                    </td>

Open in new window


Thanks.
0
Comment
Question by:aspnet-scotland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38826332
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38826567
<td class="homepage-date" valign="top">
    <script type="text/javascript">
        var months=new Array(13);
        months[1]="January";
        months[2]="Febuary";
        months[3]="March";
        months[4]="April";
        months[5]="May";
        months[6]="June";
        months[7]="July";
        months[8]="August";
        months[9]="September";
        months[10]="October";
        months[11]="November";
        months[12]="December";
        var day=new Date();
        var lmonth=months[day.getMonth() + 1];
        var date=day.getDate();
        var year = day.getFullYear();
        document.write(date + " " + lmonth + " " + year + ",");
    </script>
</td>
<td class="homepage-time" valign="top">
    <script type="text/javascript">
        function getClockTime()
        {
            var now    = new Date();
            var hour   = now.getHours();
            var minute = now.getMinutes();
            var second = now.getSeconds();
            var ap = "<span class='company-homepagetime-ampm'>&nbsp;</span>";
            if (minute < 10) { minute = "0" + minute; }
            if (hour < 10) { hour = "0" + hour; }
            var timeString = hour + ':' + minute + " " + ap;
            return timeString;
        } // function getClockTime()
            var clockTime = getClockTime();
            document.write(clockTime);
    </script>
</td>

Open in new window

0
 

Author Comment

by:aspnet-scotland
ID: 38828588
LeaKim971,

Thanks for your code, looks great. However, is it also possible to get "28th January 2013, 20:49"? i.e. 28th

Thanks
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 82

Expert Comment

by:leakim971
ID: 38828622
Line 20 replace the first " " by "th "
0
 

Author Comment

by:aspnet-scotland
ID: 38830845
But obviously "th" won't always be the case, i.e. 1st, 2nd, 3rd etc...

Thanks.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38830860
so replace line 20 by the following :
if(date==1) xx = "st "
else if(date==2) xx = "nd "
else if(date==3) xx = "rd "
// etc...
else xx = "th ";
        document.write(date + xx + lmonth + " " + year + ",");

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

733 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