Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date Time format javascript

Posted on 2013-01-28
6
Medium Priority
?
502 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

636 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