Search notes:

Javascript: Date object

A JavaScript Date object stores a number that is to interpreted as milliseconds after (or before, if this number is negative) January 1st, 1970, UTC. Thus, this number is different from Unix time (which is a number that is to be interpreted as seconds from January 1st, 1970).

constructor

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Data constructor</title>

  <script type="text/javascript">
    function main() {

      var out = document.getElementById('out')

      var months_are_zero_based = 1; // 1 = February!!!!
      var dt = new Date (2014, months_are_zero_based, 20, 9, 17, 22);

      out.innerHTML = dt.toString() + "<br>";  // Thu Feb 20 2014 09:17:22 GMT+0100

      dt = new Date ('2014-08-28')
      out.innerHTML += dt.toString() + "<br>"; // Thu Aug 28 2014 02:00:00 GMT+0200



    }
  </script>
</head>
<body onload='main();'>

  <div id='out'>
  </div>

</body>
</html>
Github repository about-javascript, path: /objects/Date/constructor.html

getDate() and getDay()

getDay() returns the date's day of the week while getDate() returns the date's day of the month.

getYear() and getFullYear()

Apparently, getYear() is depracted; getFullYear() should be used instead.

getTime()

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Date.getTime()</title>

  <script type="text/javascript">
    
    function main() {
      document.getElementById('out').innerHTML= new Date().getTime() + ' milliseconds since January 1st 1970';
    }

  </script>

</head>
<body onload='main()';>

  <div id='out'></div>

</body>
</html>
Github repository about-javascript, path: /objects/Date/getTime.html

parse()

The use Date.parse() is discouraged because different browsers implemented this function inconsistently.
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Date.parse</title>

  <script type="text/javascript">
    
    function main() {
    
       var out = document.getElementById('out');

       ms = Date.parse('1970-01-02'); // Milliseconds since Jan 1st 1970

       out.innerHTML += ms + "<br>";  // = a000 * 24 * 60 * 60

    }

  </script>

</head>
<body onload='main()';>

  <div id='out'></div>

</body>
</html>

Github repository about-javascript, path: /objects/Date/parse.html

toString()

<!DOCTYPE HTML>
<html>
<head>
  <title>Javasctipt Date.toString()</title>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">

  <script type="text/javascript">

    function init() {

      var out = document.getElementById('out');

      var dat = new Date();

      out.innerHTML = dat.toString();  // for example: «Sun Jul 13 2014 19:53:37 GMT+0200»
    }

  </script>
</head>
<body onload="init();">

  <div id="out"></div>

</body>
</html>


Github repository about-javascript, path: /objects/Date/toString.html

See also

Date arithmetic
The toISOString() method returns an ISO 8601 representation of the date stored in the object.
objects

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759391036, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/JavaScript/code-snippets/objects/Date/index(186): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78