Results 1 to 6 of 6

Thread: real time date and time in javascript

  1. #1
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default real time date and time in javascript

    Hi

    I have the following script to display real time date and time and works perfect but how do I add th or st or nd depending on the date, for example it currently read Monday January 9 2017 16:31:49 but be good if could be Monday January 9th 2017 16:31:49

    The code is below

    Code:
    function date_time(id)
    {
            date = new Date;
            year = date.getFullYear();
            month = date.getMonth();
            months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
            d = date.getDate();
            day = date.getDay();
            days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
            h = date.getHours();
            if(h<10)
            {
                    h = "0"+h;
            }
            m = date.getMinutes();
            if(m<10)
            {
                    m = "0"+m;
            }
            s = date.getSeconds();
            if(s<10)
            {
                    s = "0"+s;
            }
    		
    		function nth(d) {
          if(d>3 && d<21) return 'th'; // thanks kennebec
          switch (d % 10) {
                case 1:  return "st";
                case 2:  return "nd";
                case 3:  return "rd";
                default: return "th";
            }
        }
    		
            result = ''+days[day]+' '+months[month]+' '+d+' '+year+' '+h+':'+m+':'+s;
            document.getElementById(id).innerHTML = result;
    		
            setTimeout('date_time("'+id+'");','1000');
            return true;
    }
    Thank you in advance

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Code:
    result = ''+days[day]+' '+months[month]+' '+d+nth(d)+' '+year+' '+h+':'+m+':'+s;
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Thank you so much really appreciate the reply and help

  4. #4
    Join Date
    Jan 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yep, the Beverleyh answer is correct but that code... it's weird.

    I have some advice for you:
    Have a look these opensources libraries, Moment.js and Date.js!
    All you need to better manage the dates.

  5. #5
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by SerDrake View Post
    Yep, the Beverleyh answer is correct but that code... it's weird.

    I have some advice for you:
    Have a look these opensources libraries, Moment.js and Date.js!
    All you need to better manage the dates.
    I did look at moment.js but was unsure if it is real time so the seconds rather than having to refresh the page so get the current time

  6. #6
    Join Date
    Jan 2017
    Location
    Ahmedabad
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,
    you should try this code:
    Code:
    $(document).ready(function() {
        var interval = setInterval(function() {
            var momentNow = moment();
            $('#date-part').html(momentNow.format('YYYY MMMM DD') + ' '
                                + momentNow.format('dddd')
                                 .substring(0,3).toUpperCase());
            $('#time-part').html(momentNow.format('A hh:mm:ss'));
        }, 100);
    });
    Thanks,
    sonvi.belani
    Last edited by jscheuer1; 01-12-2017 at 02:43 PM. Reason: format

Similar Threads

  1. Real Time Calculations Using Javascript
    By gazza58 in forum JavaScript
    Replies: 1
    Last Post: 05-22-2014, 09:57 AM
  2. Modify Date and Time javascript
    By kamanburger in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 06-19-2011, 07:53 AM
  3. Subtracting date and time - javascript or asp ?
    By brenkster in forum JavaScript
    Replies: 0
    Last Post: 03-16-2011, 07:42 PM
  4. real-time cooking with javascript
    By VatsaL in forum The lounge
    Replies: 1
    Last Post: 01-26-2007, 06:09 AM
  5. [DHTML] Real Time Syntax Highlighting JavaScript
    By fermads in forum Submit a DHTML or CSS code
    Replies: 12
    Last Post: 08-20-2006, 08:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •