Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Time and date

Time and date

Scheduled Pinned Locked Moved JavaScript
tools
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    Am not sure what I’ve done wrong. The below code is supposed to show the time and date as Time|Date: 16:08:24 31/3/2013, however its now showing up as Time|Date: 16:08:24 0/3/2013…

    <script>
    function startTime()
    {
    var today=new Date();
    var d=today.getDay(); //---Day Sunday
    var dt=today.getDate(); //---Date 1-31
    var mt=today.getMonth() + 1; //---Month
    var y=today.getFullYear();//---Year
    var h=today.getHours(); //---Hours
    var m=today.getMinutes(); //---Mintutes
    var s=today.getSeconds(); //---Seconds
    // add a zero in front of numbers<10
    m=checkTime(m);
    s=checkTime(s);
    document.getElementById('txt').innerHTML= "Time|Date:"+" "+h+":"+m+":"+s+ " " + d+"/"+mt+"/"+y;
    t=setTimeout(function(){startTime()},500);
    }

    function checkTime(i)
    {
    if (i<10)
    {
    i="0" + i;
    }
    return i;
    }
    </script>

    Note: Everything else is fine except for the date, what with it bring up '0' instead of '31'

    D 1 Reply Last reply
    0
    • T Tichaona J

      Am not sure what I’ve done wrong. The below code is supposed to show the time and date as Time|Date: 16:08:24 31/3/2013, however its now showing up as Time|Date: 16:08:24 0/3/2013…

      <script>
      function startTime()
      {
      var today=new Date();
      var d=today.getDay(); //---Day Sunday
      var dt=today.getDate(); //---Date 1-31
      var mt=today.getMonth() + 1; //---Month
      var y=today.getFullYear();//---Year
      var h=today.getHours(); //---Hours
      var m=today.getMinutes(); //---Mintutes
      var s=today.getSeconds(); //---Seconds
      // add a zero in front of numbers<10
      m=checkTime(m);
      s=checkTime(s);
      document.getElementById('txt').innerHTML= "Time|Date:"+" "+h+":"+m+":"+s+ " " + d+"/"+mt+"/"+y;
      t=setTimeout(function(){startTime()},500);
      }

      function checkTime(i)
      {
      if (i<10)
      {
      i="0" + i;
      }
      return i;
      }
      </script>

      Note: Everything else is fine except for the date, what with it bring up '0' instead of '31'

      D Offline
      D Offline
      dusty_dex
      wrote on last edited by
      #2

      You have used d instead of dt in getElementById line. Sunday = 0

      "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups