Solved

Date Time format javascript

Posted on 2013-01-28
6
457 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

910 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

24 Experts available now in Live!

Get 1:1 Help Now