JavaScript

Date Objects

Dates & Times Are Tricky

  • Complicated Timezones
  • Daylight Savings
  • Date Format
    day/month/year, month/day/year, year-month-day
  • Leap Seconds
  • Differing Calendars

JavaScript Date Object

  • Represents a single moment in time
  • Based on number of milliseconds since midnight 1st January 1970 UTC
  • Standard built-in object

Creating a Date Object

/* 
Create a JavaScript Date object 
for the current date and time
*/
var today = new Date(); 

/*
Different ways to create a Date object
representing 03:24 on November 17th, 1995
*/

// Parsing from an ISO-8601 format String
var isoParseDate = new Date('1995-11-17T03:24:00');

// Integer parameters for year, month, date, hours, minutes, seconds
var intParamDate = new Date(1995, 11, 17, 3, 24, 0);

JavaScript Date Methods

  • Methods for manipulating date according to local
    time and according to universal time (UTC) 
  • getDate, getUTCDate, setDate, setUTCDate,
    getMonth, getUTCMonth, setMonth, setUTCMonth, etc.

getYear and setYear

  • Deprecated Methods
  • Only work for years between 1900 and 1999
  • Designed for two digit year representation

Year 2000 Problem

The JavaScript Solution

  • Use getFullYear/getUTCFullYear & setFullYear/setUTCFullYear Instead
  • Works For Years Between 1000 and 9999

A Quick Demo

JavaScript Objects

By John Stafford

JavaScript Objects

A cautionary tale

  • 403