add date class
This commit is contained in:
		
							
								
								
									
										126
									
								
								5.10Dateandtime/t.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								5.10Dateandtime/t.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,126 @@ | ||||
| console.log("====================Date and Time===================="); | ||||
| console.log("Date and time are created using new Date() constructor"); | ||||
| console.log("No arguments - current date and time"); | ||||
| let now = new Date(); | ||||
| console.log(now); | ||||
| console.log("Date(milliseconds) - creates a date object for the given milliseconds since Jan 1, 1970 UTC+0"); | ||||
| let Jan01_1970 = new Date(0); | ||||
| console.log(Jan01_1970); | ||||
| console.log("add 24 hours to it"); | ||||
| let Jan02_1970 = new Date(24 * 3600 * 1000); | ||||
| console.log(Jan02_1970); | ||||
| console.log("Date(string) - creates a date object from the string"); | ||||
| console.log(new Date("2017-01-26")); | ||||
| console.log("new Date(year, month, date, hours, minutes, seconds, ms) - create a date with the given components in the local time zone"); | ||||
| console.log(new Date(2011, 0, 1, 0, 0, 0, 0)); | ||||
|  | ||||
| console.log("Date methods"); | ||||
| console.log("getFullYear() - get the year (4 digits)"); | ||||
| console.log(now.getFullYear()); | ||||
| console.log("getMonth() - get the month, from 0 to 11"); | ||||
| console.log(now.getMonth()); | ||||
| console.log("getDate() - get the day of the month, from 1 to 31"); | ||||
| console.log(now.getDate()); | ||||
| console.log("getHours(), getMinutes(), getSeconds(), getMilliseconds()"); | ||||
| console.log(now.getHours()); | ||||
|  | ||||
| console.log("getDay() - get the day of the week, from 0(Sunday) to 6(Saturday)"); | ||||
| console.log(now.getDay()); | ||||
| console.log("getTime() - returns the timestamp for the date - the number of milliseconds passed from the January 1, 1970 UTC+0"); | ||||
| console.log(now.getTime()); | ||||
| console.log("getTimezoneOffset() - returns the difference between UTC and the local timezone in minutes"); | ||||
| console.log(now.getTimezoneOffset()); | ||||
| console.log("setFullYear(year, [month], [date]) - set the year"); | ||||
| console.log(now.setFullYear(2020)); | ||||
| console.log(now); | ||||
| console.log("setMonth(month, [date]) - set the month"); | ||||
| console.log(now.setMonth(0)); | ||||
| console.log(now); | ||||
| console.log("setDate(date) - set the day of the month"); | ||||
| console.log(now.setDate(1)); | ||||
| console.log(now); | ||||
|  | ||||
| console.log("automatic correction"); | ||||
| let date = new Date(2013, 0, 32); | ||||
| console.log(date); | ||||
| date.setDate(date.getDate() + 2); | ||||
| console.log(date); | ||||
| date.setSeconds(date.getSeconds() + 70); | ||||
|  | ||||
| console.log("Date can be subtracted from each other"); | ||||
| let start = new Date(); | ||||
| for (let i = 0; i < 100000; i++){ | ||||
|     let doSomething = i * i * i; | ||||
| } | ||||
| let end = new Date(); | ||||
| console.log(`The loop took ${end - start} ms`); | ||||
|  | ||||
| console.log("Date.now() - returns the current timestamp"); | ||||
| console.log(Date.now()); | ||||
| function diffSubtract(date1, date2){ | ||||
|     return date2 - date1; | ||||
| } | ||||
|  | ||||
| function diffGetTime(date1, date2) { | ||||
|     return date2.getTime() - date1.getTime(); | ||||
| } | ||||
|  | ||||
| console.log("Performance of the two methods"); | ||||
|  | ||||
| function bench(f){ | ||||
|     let date1 = new Date(0); | ||||
|     let date2 = new Date(); | ||||
|     let start = Date.now(); | ||||
|     for (let i = 0; i < 100000; i++){ | ||||
|         f(date1, date2); | ||||
|     } | ||||
|     return Date.now() - start; | ||||
| } | ||||
| console.log(`Time of diffSubtract: ${bench(diffSubtract)} ms`); | ||||
| console.log(`Time of diffGetTime: ${bench(diffGetTime)} ms`); | ||||
|  | ||||
| console.log("Date.parse(datestring) - returns timestamp"); | ||||
| console.log(Date.parse("2012-01-26T13:51:50.417-07:00")); | ||||
|  | ||||
| console.log("Task1 - Create a date"); | ||||
| let date1 = new Date(2012, 1, 20, 3, 12); | ||||
| console.log(date1) | ||||
|  | ||||
| function getWeekDay(date){ | ||||
|     day = date.getDay(); | ||||
|     m = { | ||||
|         1: 'MO', | ||||
|         2: 'TU', | ||||
|         3: 'WE', | ||||
|         4: 'TH', | ||||
|         5: 'FR', | ||||
|         6: 'SA', | ||||
|         7: 'SU', | ||||
|     } | ||||
|     return m[day]; | ||||
| } | ||||
|  | ||||
| date = new Date(2012, 0, 3); | ||||
| console.log(getWeekDay(date)) | ||||
|  | ||||
| function getDateAgo(date, days){ | ||||
|     return new Date(date.getTime() - days * 24 * 3600 * 1000); | ||||
| } | ||||
| date = new Date(2015, 0, 2); | ||||
| console.log(getDateAgo(date, 1)) | ||||
| console.log(getDateAgo(date, 2)) | ||||
| console.log(getDateAgo(date, 365)) | ||||
|  | ||||
| function getLastDayOfMonth(year, month){ | ||||
|     let date1 = new Date(year, month + 1, 0, 0, 0, 0, 0); | ||||
|     return new Date(date1.setDate(date1.getDate() )).getDate() | ||||
| } | ||||
|  | ||||
| console.log(getLastDayOfMonth(2012, 1)) | ||||
|  | ||||
| function getSecondsToday(){ | ||||
|     let date = new Date() | ||||
|     return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds(); | ||||
| } | ||||
|  | ||||
| console.log(getSecondsToday()); | ||||
		Reference in New Issue
	
	Block a user