Javascript Tutorial 17 Date Formats

There are generally 4 types of JavaScript date input formats:

Type Example
ISO Date “2015-03-25” (The International Standard)
Short Date “03/25/2015”
Long Date “Mar 25 2015” or “25 Mar 2015”
Full Date “Wednesday March 25 2015”

The ISO format follows a strict standard in JavaScript. The other formats are not so well defined and might be browser specific.

In some browsers, months or days with no leading zeroes may produce an error:

var d = new Date("2015-3-25");

The behavior of “YYYY/MM/DD” or “DD-MM-YYYY” is undefined. Some browsers will try to guess the format. Some will return NaN.

var d = new Date("2015/03/25");
var d = new Date("25-03-2015");

Independent of input format, JavaScript will (by default) output dates in full text string format:

Wed Mar 25 2015 09:00:00 GMT+0900 (KST)

Javascript Tutorial 16 Dates

A JavaScript date can be written as a string:

Tue Jun 27 2017 14:24:16 GMT+0900 (KST)

or as a number:

1498541056831

Dates written as numbers, specifies the number of milliseconds since January 1, 1970, 00:00:00.

There are 4 ways of initiating a date:

new Date()                // current date and time
new Date(milliseconds)    // zero time plus the number
new Date(dateString)      // specified date and time
new Date(year, month, day, hours, minutes, seconds, milliseconds) // specified date and time

JavaScript counts months from 0 to 11. January is 0. December is 11.

var d = new Date(99, 5, 24, 11, 33, 30, 0);
var d = new Date(99, 5, 24);  // the last 4 parameters can be omitted

When setting a date, without specifying the time zone, JavaScript will use the browser’s time zone.

When getting a date, without specifying the time zone, the result is converted to the browser’s time zone.

Javascript Tutorial 15 Numbers Methods

toFixed() returns a string, with the number written with a specified number of decimals:

var x = 9.656;
x.toFixed(0);           // returns 10
x.toFixed(2);           // returns 9.66
x.toFixed(4);           // returns 9.6560
x.toFixed(6);           // returns 9.656000

There are 3 JavaScript methods that can be used to convert variables to numbers:

  • The Number() method
  • The parseInt() method
  • The parseFloat() method These methods are not number methods, but global JavaScript methods.

Number() can be used to convert JavaScript variables to numbers:

x = true;
Number(x);        // returns 1
x = false;     
Number(x);        // returns 0
x = new Date();
Number(x);        // returns 1404568027739
x = "10"
Number(x);        // returns 10
x = "10 20"
Number(x);        // returns NaN

parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:

parseInt("10");         // returns 10
parseInt("10.33");      // returns 10
parseInt("10 20 30");   // returns 10
parseInt("10 years");   // returns 10
parseInt("years 10");   // returns NaN 

parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:

parseFloat("10");        // returns 10
parseFloat("10.33");     // returns 10.33
parseFloat("10 20 30");  // returns 10
parseFloat("10 years");  // returns 10
parseFloat("years 10");  // returns NaN