Date Objects, toUTCString & toDateString in javascript

Hello friends, In this article, we will learn about Date Object, toUTCString and toDateString methods in javascript. We will create Date objects in all possible ways.

Date objects are created with the new Date() constructor. JavaScript use browser's time zone by default and display a date in full text string.

We can create date objects in four different ways.

1) new Date()

var d = new Date(); document.write(d);

OUTPUT: Thu Jun 20 2019 13:12:56 GMT+0530 (India Standard Time)

2) new Date(date string)

var d = new Date("May 17, 2019 16:59:59"); document.write(d);

OUTPUT: Fri May 17 2019 16:59:59 GMT+0530 (India Standard Time)

3) new Date(milliseconds)

var d = new Date(0); document.write(d);

OUTPUT: Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)

01 January 1970 + 100000000000 milliseconds

var d = new Date(100000000000); document.write(d);

OUTPUT: Sat Mar 03 1973 15:16:40 GMT+0530 (India Standard Time)

January 01 1970 - 100000000000 milliseconds

var d = new Date(-100000000000); document.write(d);

OUTPUT: Mon Oct 31 1966 19:43:20 GMT+0530 (India Standard Time)

One Day 24 Hours

var d = new Date(86400000);

OUTPUT: Fri Jan 02 1970 05:30:00 GMT+0530 (India Standard Time)

4) new Date(year, month, day, hours, minutes, seconds, milliseconds)

var d = new Date(2019, 05, 17, 10, 33, 30, 0); document.write(d);

OUTPUT: Mon Jun 17 2019 10:33:30 GMT+0530 (India Standard Time)

With One parameter:

NOTE:  If you supply only one parameter it will be treated as milliseconds.

var d = new Date(2019); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Thu Jan 01 1970 05:30:02 GMT+0530 (India Standard Time) Thu, 01 Jan 1970 00:00:02 GMT Thu Jan 01 1970

With two parameter:

Last day of the Month.

var d = new Date(2019,06); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Mon Jul 01 2019 00:00:00 GMT+0530 (India Standard Time) Sun, 30 Jun 2019 18:30:00 GMT Mon Jul 01 2019

With three parameters:

var d = new Date(2019,06,20); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Sat Jul 20 2019 00:00:00 GMT+0530 (India Standard Time) Fri, 19 Jul 2019 18:30:00 GMT Sat Jul 20 2019

With four parameters:

var d = new Date(2019,06,20, 15); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Sat Jul 20 2019 15:00:00 GMT+0530 (India Standard Time) Sat, 20 Jul 2019 09:30:00 GMT Sat Jul 20 2019

With five parameters:

var d = new Date(2019,06,20, 15, 30); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Sat Jul 20 2019 15:30:00 GMT+0530 (India Standard Time) Sat, 20 Jul 2019 10:00:00 GMT Sat Jul 20 2019

With six parameter:

var d = new Date(2019,06,20, 15, 30, 15); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Sat Jul 20 2019 15:30:15 GMT+0530 (India Standard Time) Sat, 20 Jul 2019 10:00:15 GMT Sat Jul 20 2019

With seven parameter:

var d = new Date(2019,06,20, 15, 30, 15, 59); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Sat Jul 20 2019 15:30:15 GMT+0530 (India Standard Time) Sat, 20 Jul 2019 10:00:15 GMT Sat Jul 20 2019

Note:  One and two digit years will be interpreted as 19xx:

var d = new Date(19,06,15, 20, 45); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Tue Jul 15 1919 20:45:00 GMT+0530 (India Standard Time) Tue, 15 Jul 1919 15:15:00 GMT Tue Jul 15 1919

var d = new Date(9,06,15, 20, 45); document.write(d+"<br />"); document.write(d.toUTCString()+"<br />"); document.write(d.toDateString()+"<br />");

OUTPUT: Thu Jul 15 1909 20:45:00 GMT+0530 (India Standard Time) Thu, 15 Jul 1909 15:15:00 GMT Thu Jul 15 1909

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories