Solved

Date Time format javascript

Posted on 2013-01-28
6
438 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
Comment Utility
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
<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
Comment Utility
LeaKim971,

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

Thanks
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Line 20 replace the first " " by "th "
0
 

Author Comment

by:aspnet-scotland
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

771 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

10 Experts available now in Live!

Get 1:1 Help Now