Is it possible to determine if a date is a Saturday or Sunday using JavaScript?
Do you have the code for this?
var today = new Date(); var startDay = 0; var weekStart = new Date(today. getDate() - (7 + today. getDay() - startDay) % 7); var weekEnd = new Date(today. getDate() + (7 - today.
We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax. Syntax: new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hours, minutes, seconds, milliseconds);
now() The static Date. now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
Sure it is! The Date
class has a function called getDay()
which returns a integer between 0 and 6 (0 being Sunday, 6 being Saturday). So, in order to see if today is during the weekend:
var today = new Date(); if(today.getDay() == 6 || today.getDay() == 0) alert('Weekend!');
In order to see if an arbitrary date is a weekend day, you can use the following:
var myDate = new Date(); myDate.setFullYear(2009); myDate.setMonth(7); myDate.setDate(25); if(myDate.getDay() == 6 || myDate.getDay() == 0) alert('Weekend!');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With