|
11 | 11 | | [Setting Timers and Intervals](#setting-timers-and-intervals) | |
12 | 12 | | [The `location` and `history` Objects](#the-location-and-history-objects) | |
13 | 13 | | [The `navigator` Object](#the-navigator-object) | |
| 14 | +| [Working with Dates](#working-with-dates) | |
14 | 15 |
|
15 | 16 | Below topics will be covered and for its practical application, [Planner Project](project_planner/) will be used as reference where concept of classes and functions are demonstrated: |
16 | 17 |
|
@@ -354,3 +355,44 @@ Readings: |
354 | 355 | - [Window: navigator property](https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator#example_1_browser_detect_and_return_a_string) |
355 | 356 |
|
356 | 357 | - [JavaScript Navigator](https://www.javascripttutorial.net/javascript-bom/javascript-navigator/) |
| 358 | + |
| 359 | +## Working with [Dates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| 360 | + |
| 361 | +In JavaScript, the `Date()` function is used to create a new Date object, which represents a specific date and time. |
| 362 | + |
| 363 | +Here are some common methods of the `Date` object: |
| 364 | + |
| 365 | +- [`getDate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate): Returns the day of the month (from 1-31). |
| 366 | +- [`getMonth()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth): Returns the month (from 0-11). |
| 367 | +- [`getFullYear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear): Returns the year (four digits). |
| 368 | +- [`getHours()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours): Returns the hour (from 0-23). |
| 369 | +- [`getMinutes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes): Returns the minutes (from 0-59). |
| 370 | +- [`getSeconds()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getSeconds): Returns the seconds (from 0-59). |
| 371 | +- [`getTime()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime): Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. |
| 372 | + |
| 373 | +Here's an example of how to create a new `Date` object and get the current date and time: |
| 374 | + |
| 375 | +```javascript |
| 376 | +// Create a new Date object |
| 377 | +const currentDate = new Date(); |
| 378 | + |
| 379 | +// Get the current year |
| 380 | +const currentYear = currentDate.getFullYear(); |
| 381 | + |
| 382 | +// Get the current month |
| 383 | +const currentMonth = currentDate.getMonth(); |
| 384 | + |
| 385 | +// Get the current day |
| 386 | +const currentDay = currentDate.getDate(); |
| 387 | + |
| 388 | +// Get the current hour |
| 389 | +const currentHour = currentDate.getHours(); |
| 390 | + |
| 391 | +// Get the current minute |
| 392 | +const currentMinute = currentDate.getMinutes(); |
| 393 | + |
| 394 | +// Get the current second |
| 395 | +const currentSecond = currentDate.getSeconds(); |
| 396 | +``` |
| 397 | + |
| 398 | +Note that the `Date()` function uses the local time zone of the user's computer. If you need to work with a specific time zone or date format, you may need to use a library like `moment.js` or perform manual calculations. |
0 commit comments