1. Constructors

2. new Date(milliseconds): Creates a Date given the number of milliseconds passed from January 1, 1970 00:00:00, GMT+0. Note, millisecond is 1/1000 of second

1

2

3

// 24 hours after Jan 1, 1970

varJan02_1970=newDate(3600*24*1000);

console.log(Jan02_1970);

3. new Date(datestring): If the single argument is a string. See Date.parse method for the format.

4. new Date(year, month [, day, hours, minutes, seconds, ms]): The date can be created given it’s components in the current time zone. For this format there should be at least two first arguments, next ones are optional.

P.S. Make sure that leading zeroes are at place, so 1 Jan 2011 would become 01.01.11, not 1.1.11.

Solution

The day is extracted by d.getDate(). To follow the format, days less than 10 are prepended with 0:

1

2

vardd=d.getDate();

if(dd<10)dd='0'+dd;

d.getMonth() returns the month beginning from 0, so we need to increment it by one:

1

2

varmm=d.getMonth()+1;// now moths are 1-12

if(mm<10)mm='0'+mm;

The year is returned in 4-digit form. To make it 2-digit, use the modulo operator:

1

2

varyy=d.getFullYear()%100;

if(yy<10)yy='0'+yy;

>Note that the year just like other components needs to be left-padded with zero.

The full code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

functionformatDate(d){

vardd=d.getDate()

if(dd<10)dd='0'+dd;

varmm=d.getMonth()+1

if(mm<10)mm='0'+mm;

varyy=d.getFullYear()%100

if(yy<10)yy='0'+yy;

returndd+'.'+mm+'.'+yy;

}

vard=newDate(2011,0,30)// 30 Jan 2011

console.log(formatDate(d))// '30.01.11'

Solution

The general scheme is to create a string: “

”, and then assign to innerHTML.

Create d = new Date(year, month-1). It is set to the first day of the month.

Fill the first row with empty cells, to show days before d.getDay()

While d is at same month, increment it’s month day with d.setDate(d.getDate()+1) and add the cell into table.

When the day is last in the week (check by d.getDay() % 7 == 6), split the table by adding "

"

Finish the table by empty cells.

See the detais below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

functionCalendar(id,year,month){

varelem=document.getElementById(id);

varmon=month-1// (1);

vard=newDate(year,mon);

vartable=['<table><tr>'];

// (2) fill first row

// 0 1 2 3 4 5 6

// 29 30 31| 1 2 3 4

for(vari=0;i<d.getDay();i++){

table.push('<td></td>');

}

// main body (3)

while(d.getMonth()==mon){

table.push('<td>'+d.getDate()+'</td>')

if(d.getDay()%7==6){// (4)

table.push('</tr><tr>');

}

d.setDate(d.getDate()+1);

}

// (5) table tail

for(vari=d.getDay();i<7;i++){

table.push('<td></td>');

}

table.push('</tr></table>');

elem.innerHTML=table.join('\n');

}

Parsing from string, Date.parse

Parsing from and to simplified ISO 8601 Extended format is implemented in most browsers excepts IE<9.

The format is: YYYY-MM-DDTHH:mm:ss.sssZ. The ‘Z’ part denotes an optional time zone. Subforms are also possible:

1

2

3

4

5

6

YYYY

YYYY-MM

YYYY-MM-DD

THH:mm// example: 'T12:00'

THH:mm:ss

THH:mm:ss.sss

There is a method Date.parse(str) which parses the str into Date object.

If parsing fails, Date.parse returns NaN.

The example below works correctly in recent browsers except IE<9 which uses it's own non-standard format for Date.parse:

1

2

3

4

vard=Date.parse('2011-01-26T13:51:50.417');

// d = Date

console.log(d);

Prior to ECMAScript 5th edition, there were no Date string format, so browsers invented formats of their own.
The following older non-ISO format is accepted by all browsers including IE:

1

2

3

vard=Date.parse("January 26, 2011 13:51:50");

console.log(d);

You can read more about other non-ISO variants supported by IE in MSDN Date.parse documentation and find the one you want to use. But you have to check the chosen format against all browsers, because it’s non-standard.