9. JS Date Object Public

9. JS Date Object

Kostas Diakogiannis
Course by Kostas Diakogiannis, updated more than 1 year ago Contributors

Description

Js Intro to date object

Module Information

No tags specified

Context

Course's Objectives 1. To learn how to work with dates and times through the Date object. 2. To learn the different ways someone can instantiate a new Date object. The different formats. 3. To learn how you can access specific part of a date info though date 'get' methods. 4. To learn how you can alter, change and modify an existing Date object through all the 'set' methods. 5. To learn the filter array method.
Show less
No tags specified

Context

Creating a new Date Object Date objects are native JavaScript objects that we can use in order to work with dates and times. Exactly like the Math object, the Date object has multiple functions that solve problems regarding date issues.  For example when you have a booking flight or hotel website,  and you want  to force the user to select a specific date from a date field form, or if you want to just depict a countdown from now to a specific date. There are 4 ways to create a new date object as shown above. First you need to create a variable and assign a new Date instance there. 1. const currentDate = new Date() // This will create an object with the date and time of creation. 2. const myBirthday = new Date(year, month, day) => example new Date(1988, 02, 23). It stores the date of 23rd of March 1988.  More parameters can be put for giving and storing an exact date like hours (0-23), minutes(1-60), seconds etc. * Yes March is no mistake, the month's counting starts from 0! 3. const myBirthday = new Date('23 Mar 1988") // See more about this option next page! 4. const myBirthday = new Date(17534835345) // The number of milliseconds that have passed since 1.1.1970 // Used for short term date or time differences. Script's execution performance etc.   Find more here:
Show less
No tags specified

Context

Create Dates by passing strings Dates can also be created by passing a string. (The third option to the previous page) Someone must take care though, that only specific formats are accepted when defining a date with these methods. The commonest are being shown above.
Show less
No tags specified

Context

Getting info from a Date object Exactly as you might have imagined,  we can examine existing date objects after they have been created. Thus we can check if a date is a specific one, what day of the week is it, compare dates (which one is before the other one?) and many many more.  All the date objects for accessing info (and NOT altering it) are being shown above. Attention: These are READ-ONLY methods so you can display slices of information from an existing date object. For actually changing date objects' piece of info, proceed to the next page.
Show less
No tags specified

Context

D-1 Book future day event Create a function that accepts two Date objects as arguments. The first one is the current date (today) and the second one a future date that the user wants to book a seat on an event you are organizing. Compare the dates, and if the second date is a future date then print ('Your book to the event for the 25th April 2019 is already guaranteed!')  given that this is the future date. Use some of the methods you learned to extract the info in order to construct your message nicely. If it is not a future date print a message that informs the user that he should select a future date. hint: Future dates are bigger than previous dates in comparison operations
Show less
No tags specified

Context

D-2 Is it a weekend? Create a function that accepts a specific date given as an argument and returns a message if the day of the specific date is a weekend day or not. You may want to use the getDay function to have access to which day it is. This returns a number from 0 to 6. 0 Sunday, 1 is Monday, 6 is for Saturday etc. You may find this info useful if you want to check what day a specific date is.
Show less
No tags specified

Context

Date setting methods Like getting methods for accessing information, setting methods are used in order to change, modify or alter an existing date object. You can alter the date(1-31), year, month, the hours though milliseconds and many more. Once the function is executed the suitable part of the date is also updated. You can recheck your date now.
Show less
No tags specified

Context

D-3 How many days remain? Create a function that accepts a date object as an argument and calculates how many days remain until the end of the month. The function should return the number of days.
Show less
No tags specified

Context

D-4 Last day of each month Write a function that calculates what is the last day of each month on a given year.  Pass the given year as an argument to make this the most reusable way you can. example: getLastDayForEachMonth(2018) should print to console: Last day of January for 2018 is Tuesday, Last day of February for 2018 is Monday, Last of March for 2018 is Friday...  etc.
Show less
No tags specified

Context

D-5 Countdown to Christmas Create a function that calculates exactly how many milliseconds is the current Date away from Christmas Date ("Dec 25 2019"). You can subtract these days and you will get the difference in milliseconds.  Then find a way to format this result to human readable like ('8 months, 27 days, 10 hours, 45 minutes and 35 seconds'). Extra bonus: Update this every second by calling this function within a setInterval.
Show less
No tags specified

Context

D-6 Create the Zodiac cycle Write a function that accepts an argument as a birthday date and returns the correct  zodiac sign as a string.  Create 12 objects that each has three properties. The sign property, which is the string like 'Aries', 'Capricorn', etc. The begin date for this zodiac, and the end date of the zodiac cycle. Then if the birthday argument is between (smaller than endDate AND bigger than beginDate) console.log the corresponding sign! Hint: You may want to loop through all the objects to check if the date is between each object's dates.  That means you have to put all the objects inside a place, where you can loop. :-)
Show less
Show full summary Hide full summary