Solved

Date Time format javascript

Posted on 2013-01-28
6
494 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

691 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