With JavaScripts Data Object we can access the current date and create custom date to trigger special events. Read on to see exactly play with the year, month, day etc to achieve different results
Days of the week are accessed though JavaScript’s ‘Date()’ object with the ‘getDay()’ method – which returns an array item.
0 = sunday
1 = monday
2 = tuesday
and so on.
//lets make new an instance of the Data object var d=new Date(); var current_day = d.getDay(); //lets create a switch condition loop to see what current_day is switch(current_day) { case 0: document.write("Sunday"); break; case 1: document.write("Monday"); break; case 2: document.write("Tuesday"); break; case 3: document.write("Wednesday"); break; case 4: document.write("Thursday"); break; case 5: document.write("Friday"); break; case 6: document.write("Saturday"); break; }
in the same manner you can use the following
getTime() //
getFullYear() // get the year
setFullYear() // Set the year
toUTCString() //convert todays date to a String
Setting a new date can be done by passing in the following parameters into the Date object
leaving out a parameter will make it 0 be default
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
..or you can pass in a string
new Date("October 13, 1975 11:13:00")
Now lets look at creating a Custom Date Object. With a custom date object we can set special events on webpages and create countdown timers.
for the reason of me not knowing when the article will be read The event time will be set one year ahead of your system time.
<script> //get todays date var current_date = new Date(); var current_year = current_date.getFullYear(); var current_month = current_date.getMonth(); var current_day = current_date.getDay(); var special_event_date = new Date(current_year + 1, 8, 2) //current year plus 1, august, tuesday var special_event_year = special_event_date.getFullYear(); var special_event_month = special_event_date.getMonth(); var special_event_day = special_event_date.getDay(); var years_till_event = special_event_year - current_year; var months_till_event = special_event_month - current_month; var days_till_event = special_event_day - current_day; document.write("Time Until Special Event :"+ years_till_event +" Years, "+ months_till_event + " Months and " +days_till_event+" days."); </script>
you can also do something like this
<script> var current_date = new Date(); var special_event_date = new Date(2012, 1, 1) if(current_date >= special_event_date) { document.write("THERE IS AN EVENT ON RIGHT NOW") } else { document.write("THERE IS AN EVENT COMING ON : " + special_event_date.getFullYear() +" /"+ special_event_date.getMonth()+"/ "+special_event_date.getDay()) } </script>
Leave A Comment