Javascript Date and Time
In javascript Date object is capable to work date and time system operation. This object are provide information about year, month, hour, minute, seconds and milliseconds. This object relative method are providing various functionality, Such as formatting date pattern, modified date and time, distance between the date and so on.
Date.prototype methods
In this section include all methods of Date objects. And given of its basic examples.
Method | Description |
---|---|
Date() | Creating a Date objects |
toString() | Date object information are converted into a string text |
toDateString() | This method are providing information of month year day and day name to an string format |
toTimeString() | This method are returning information about times (hours, minutes, seconds ,GMT) and Time standard |
toISOString() | This method are returning a string of ISO format. That is form of YYYY-MM-DDTHH:mm:ss.sssZ |
toUTCString() | This method are return a string value of date object which is form of UTC time zone |
toGMTString() | The toGMTString() method converts a given date object, according to Greenwich Mean Time (GMT), to a string and returns that result |
getDate() | This method are return a numeric value of day in a date object |
setDate() | This method are sets new value of day in a date object |
getDay() | This method are return the day of the week. 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday ,4 for Thursday, 5 for Friday, 6 for Saturday |
getFullYear() | returns the year of a string object |
setFullYear() | Set new value of year in date object |
getHours() | This method are returning a numeric value which is information about the hours in date object |
setHours() | This method are sets the new value of hour in date objects |
getMilliseconds() | This method are returns a numeric values of milliseconds in date objects |
setMilliseconds() | This method are sets a new numeric values of milliseconds in date objects |
getMinutes() | This method are returns a numeric values of minutes in date objects |
setMinutes() | This method are sets a new value of minutes in date objects |
getMonth() | This method are returning the value of month index from 0 to 11 |
setMonth() | This method are sets a new value of month in date objects |
getSeconds() | This method are returns a numeric values of second in date objects |
setSeconds() | This method are sets a new value of seconds in date objects |
getTime() | This method are return a numeric value of millisecond from of 1 January 1970 00:00:00 to current date object |
setTime() | This method are adding given millisecond value to date January 1, 1970 |
getTimezoneOffset() | This method are return the time difference between Universal Coordinated Time (UTC) and local time |
getUTCDate() | This method are returns a numeric values of day in a date object |
setUTCDate() | This method are returns a numeric values of day in a date object |
getUTCDay() | This method are returning a numeric values of a day to date object |
getUTCFullYear() | This method are returning a numeric values of a year to date object. |
setUTCFullYear() | This method are sets a numeric values of year in a given date object |
getUTCHours() | This method are returning a numeric values of a hours in given date object |
setUTCHours() | This method are sets a numeric values of hours in a given date object |
getUTCMilliseconds() | This method are returning a numeric values of a milliseconds in given date object |
setUTCMilliseconds() | This method are sets a new numeric values of milliseconds in a given date object |
getUTCMinutes() | This method are returning a numeric values of a minutes in given date object |
setUTCMinutes() | This method are sets a new numeric values of minutes in a given date object |
getUTCMonth() | This method are returning a numeric values of month in given date object |
setUTCMonth() | This method are sets a new numeric values of month in a given date object |
getUTCSeconds() | This method are returning a numeric values of second in given date object |
setUTCSeconds() | This method are sets a new numeric values of seconds in a given date object |
valueOf() | This method are get the number of milliseconds between 1 January 1970 00:00:00 UTC and the given specified date |
getYear() | Returns a result which is calculate distance of between given year to year of 1900. (given year - 1900) |
setYear() | This method are updating the value of year in a date object |
toJSON() | This method are return a string values of given date object |
toLocaleString() | This method are return a string text which contain information about date and time |
toLocaleDateString() | This method are return a string value which information about date value. their format is day/month/year |
toLocaleTimeString() | This method are return a string value which information about time |
constructor()
Date class constructor are create a new of object.
let date1 = new Date();
console.log(date1);
let date2 = new Date("2020 4 17 11:24:13");
console.log(date2);
2019-07-13T09:58:59.402Z
2020-04-17T05:54:13.000Z
toString()
This method are returns a string value of date objects.
//Create a date object
var date = new Date("2020 4 17 11:24:13");
console.log(typeof date);
console.log(date);
//Convert date object to a string
var stringText=date.toString();
console.log(typeof stringText);
console.log(stringText);
object
2020-04-17T05:54:13.000Z
string
Fri Apr 17 2020 11:24:13 GMT+0530 (India Standard Time)
toDateString()
This method are providing information of month year day and day name to an string format.
let date = new Date();
console.log(date);
console.log(date.toDateString());
2019-07-13T11:13:18.957Z
Sat Jul 13 2019
toTimeString()
This method are returning information about times (hours, minutes, seconds ,GMT) and Time standard.
let date = new Date();
console.log(date);
let info = date.toTimeString();
console.log("Type ",typeof info);
console.log("Info ",info);
2019-07-13T11:11:03.554Z
Type string
Info 16:41:03 GMT+0530 (India Standard Time)
toISOString()
This method are returning a string of ISO format. That is form of YYYY-MM-DDTHH:mm:ss.sssZ.
let date = new Date();
console.log(date.toISOString());
2019-07-13T12:45:50.945Z
toUTCString()
This method are return a string value of date object which is form of UTC time zone.
let date = new Date();
console.log(date);
console.log(date.toLocaleString());
console.log(date.toUTCString());
2019-07-13T11:29:28.700Z
7/13/2019, 4:59:28 PM
Sat, 13 Jul 2019 11:29:28 GMT
toGMTString()
The toGMTString() method converts a given date object, according to Greenwich Mean Time (GMT), to a string and returns that result.
let date = new Date();
console.log(date);
console.log(date.toGMTString());
2019-07-13T11:09:04.549Z
Sat, 13 Jul 2019 11:09:04 GMT
getDate()
This method are return a numeric value of day in a date object.
let date = new Date();
console.log(date);
let day = date.getDate();
console.log("Month Day :",day);
2019-07-13T10:29:54.368Z
Month Day : 13
setDate()
This method are sets new value of day in a date object.
let date = new Date();
console.log(date);
date.setDate(25);
console.log(date);
2019-07-13T10:32:03.107Z
2019-07-25T10:32:03.107Z
getDay()
This method are return the day of the week. 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday ,4 for Thursday, 5 for Friday, 6 for Saturday.
let date = new Date();
console.log(date);
let day = date.getDay();
console.log("Week Day :",day);
2019-07-13T10:28:04.883Z
Week Day : 6
getFullYear()
This method are return a numeric value of year in date object.
let date = new Date();
console.log(date);
let year = date.getFullYear();
console.log(year);
2019-07-13T10:06:35.110Z
2019
setFullYear()
This methods are sets a new values of year in date objects
let date = new Date();
console.log(date);
date.setFullYear(2050); //set new year
console.log(date);
2019-07-13T10:34:07.229Z
2050-07-13T10:34:07.229Z
getHours()
This method are returning a numeric value which is information about the hours in date object.
let date = new Date();
console.log(date.toTimeString());
let hours= date.getHours(); //get Hours
console.log(hours);
16:12:03 GMT+0530 (India Standard Time)
16
setHours()
This method are sets the new value of hour in date objects.
let date = new Date();
console.log(date.toTimeString());
date.setHours(9); //set Hours 9
console.log(date.toTimeString());
16:13:32 GMT+0530 (India Standard Time)
09:13:32 GMT+0530 (India Standard Time)
getMilliseconds()
This method are returns a numeric values of milliseconds in date objects.
let date = new Date();
console.log(date);
console.log(date.toTimeString());
let ms=date.getMilliseconds(); //get Milliseconds
console.log(ms);
2019-07-13T10:50:12.801Z
16:20:12 GMT+0530 (India Standard Time)
801
setMilliseconds()
This method are sets a new numeric values of milliseconds in date objects
let date = new Date();
console.log(date);
console.log(date.toTimeString());
date.setMilliseconds(500); //set Milliseconds
console.log(date);
2019-07-13T10:53:00.626Z
16:23:00 GMT+0530 (India Standard Time)
2019-07-13T10:53:00.500Z
getMinutes()
This method are returns a numeric values of minutes in date objects.
let date = new Date();
console.log(date.toTimeString());
let minutes=date.getMinutes();
console.log(minutes);
16:08:04 GMT+0530 (India Standard Time)
8
setMinutes()
This method are sets a new value of minutes in date objects.
let date = new Date();
console.log(date.toTimeString());
date.setMinutes(30); //set new minute
console.log(date.toTimeString());
16:09:53 GMT+0530 (India Standard Time)
16:30:53 GMT+0530 (India Standard Time)
getMonth()
This method are returning the value of month index from 0 to 11.
let date = new Date();
console.log(date);
let month = date.getMonth();
console.log("Index :",month);
console.log("Actual :",month+1);
2019-07-13T10:19:26.285Z
Index : 6
Actual : 7
setMonth()
This method are sets a new value of month in date objects.
let date = new Date();
console.log(date);
date.setMonth(3); //index value 3 from (0 to 11)
console.log(date);
2019-07-13T10:54:45.132Z
2019-04-13T10:54:45.132Z
getSeconds()
This method are returns a numeric values of second in date objects
let date = new Date();
console.log(date.toTimeString());
let seconds=date.getSeconds();
console.log(seconds);
16:27:34 GMT+0530 (India Standard Time)
34
setSeconds()
This method are sets a new value of seconds in date objects
let date = new Date();
console.log(date.toTimeString());
date.setSeconds(50);
console.log(date.toTimeString());
16:28:22 GMT+0530 (India Standard Time)
16:28:50 GMT+0530 (India Standard Time)
getTime()
This method are return a numeric value of millisecond from of 1 January 1970 00:00:00 to current date object.
let date = new Date();
console.log(date);
console.log(date.toTimeString());
console.log(date.getTime());
1563021728219
setTime()
This method are adding given millisecond value to date January 1, 1970.
let date = new Date();
console.log(date); //current date
console.log(date.toTimeString());
//adding the millisecond from in a date January 1, 1970
date.setTime(1750000000000);
console.log(date);
console.log(date.toTimeString());
2019-08-06T09:30:02.459Z
15:00:02 GMT+0530 (India Standard Time)
2025-06-15T15:06:40.000Z
20:36:40 GMT+0530 (India Standard Time)
getTimezoneOffset()
This method are return the time difference between Universal Coordinated Time (UTC) and local time.
let date = new Date();
console.log(date);
console.log(date.toTimeString());
console.log(date.getTimezoneOffset());
2019-07-13T12:34:19.591Z
18:04:19 GMT+0530 (India Standard Time)
-330
getUTCDate()
This method are returns a numeric values of day in a date object.
let date = new Date();
console.log(date);
console.log(date.getUTCDate());
2019-07-13T11:17:34.646Z
13
setUTCDate()
This method are returns a numeric values of day in a date object.
let date = new Date();
console.log(date);
date.setUTCDate(21)
console.log(date);
2019-07-13T11:21:47.526Z
2019-07-21T11:21:47.526Z
getUTCDay()
This method are returning a numeric values of a day to date object.
let date = new Date();
console.log(date);
console.log(date.getUTCDay()); //week day
2019-07-13T11:18:37.594Z
6
getUTCFullYear()
This method are returning a numeric values of a year to date object.
let date = new Date();
console.log(date);
console.log(date.getUTCFullYear()); //get year
2019-07-13T11:19:28.170Z
2019
setUTCFullYear()
This method are sets a numeric values of year in a given date object.
let date = new Date();
console.log(date.toLocaleDateString());
date.setUTCFullYear(2080);
console.log(date.toLocaleDateString());
7/13/2019
7/13/2080
getUTCHours()
This method are returning a numeric values of a hours in given date object.
let date = new Date();
console.log(date);
console.log(date.getUTCHours());
2019-07-13T11:36:10.686Z
11
setUTCHours()
This method are sets a numeric values of hours in a given date object
let date = new Date();
console.log(date);
date.setUTCHours(18);
console.log(date);
2019-07-13T11:37:52.140Z
2019-07-13T18:37:52.140Z
getUTCMilliseconds()
This method are returning a numeric values of a milliseconds in given date object.
let date = new Date();
console.log(date);
console.log(date.getUTCMilliseconds());
2019-07-13T11:38:35.669Z
669
setUTCMilliseconds()
This method are sets a new numeric values of milliseconds in a given date object
let date = new Date();
console.log(date);
date.setUTCMilliseconds(999);
console.log(date);
2019-07-13T11:39:34.034Z
2019-07-13T11:39:34.999Z
getUTCMinutes()
This method are returning a numeric values of a minutes in given date object.
let date = new Date();
console.log(date);
console.log(date.getUTCMinutes());
2019-07-13T11:40:24.950Z
40
setUTCMinutes()
This method are sets a new numeric values of minutes in a given date object
let date = new Date();
console.log(date);
date.setUTCMinutes(50);
console.log(date);
2019-07-13T11:41:22.896Z
2019-07-13T11:50:22.896Z
getUTCMonth()
This method are returning a numeric values of month in given date object.
let date = new Date();
console.log(date);
console.log(date.getUTCMonth()); //start month from (0-11)
2019-07-13T11:42:23.905Z
6
setUTCMonth()
This method are sets a new numeric values of month in a given date object
let date = new Date();
console.log(date);
date.setUTCMonth(1); //2nd month
console.log(date);
2019-07-13T11:43:40.399Z
2019-02-13T11:43:40.399Z
getUTCSeconds()
This method are returning a numeric values of second in given date object.
let date = new Date();
console.log(date);
console.log(date.getUTCSeconds());
2019-08-06T07:35:20.627Z
20
setUTCSeconds()
This method are sets a new numeric values of seconds in a given date object
let date = new Date();
console.log(date);
date.setUTCSeconds(10);
console.log(date);
2019-07-13T11:46:57.838Z
2019-07-13T11:46:10.838Z
valueOf()
This method are get the number of milliseconds between 1 January 1970 00:00:00 UTC and the given specified date.
let date = new Date("2 1 2020");
console.log(date);//date
console.log(date.valueOf()); //milliseconds
2020-01-31T18:30:00.000Z
1580495400000
getYear()
Returns a result which is calculate distance of between given year to year of 1900. (given year - 1900).
let date = new Date();
console.log(date);//date
console.log(date.getYear()); //2019-1900 = 119
2019-07-13T12:18:50.711Z
119
setYear()
This method are updating the value of year in a date object.
let date = new Date();
console.log(date);//date
date.setYear(2000);
console.log(date);
2019-07-13T12:20:09.349Z
2000-07-13T12:20:09.349Z
toJSON()
This method are return a string values of given date object.
let date = new Date("January 5, 2020 04:10:20");
var json=date.toJSON();
console.log(json);
2020-01-04T22:40:20.000Z
toLocaleString()
This method are return a string text which contain information about date and time.
let date = new Date();
console.log(date);
console.log(date.toLocaleString());
2019-07-13T11:03:20.340Z
7/13/2019, 4:33:20 PM
toLocaleDateString()
This method are return a string value which information about date value. their format is day/month/year.
let date = new Date();
console.log(date);
console.log(date.toLocaleDateString());
2019-07-13T11:02:26.780Z
7/13/2019
toLocaleTimeString()
This method are return a string value which information about time.
let date = new Date();
console.log(date);
console.log(date.toLocaleTimeString());
2019-07-13T11:04:49.786Z
4:34:49 PM
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment