Logo 
Search:

Javascript Article

Submit Article
Comments on How to find day of the week
Category: Javascript (Date Functions)    Author: Easy Tutor

This article will explains you how to find day of the week in javascript.

Example: If day is

Sunday - returns 0
Monday - returns 1
Tuesday - returns 2
Wednesday - returns 3
Thursday - returns 4
Friday - returns 5
Saturday - returns 6


Todd Katz
Todd Katz from United StatesDec 31
Having pieced this together from other Internet examples, I thought I'd share a rendition of getting dates for a particular period such as "the next five days".

Cheers,
Todd


<SCRIPT LANGUAGE="JavaScript1.2">
<!-- gets first day of period -->
var today = new Date();
var targetDay = new Date(today.getTime() + 0 * 24 * 60 * 60 * 1000);
var targetDayNum = today.valueOf() + 1000 * 60 * 60 * 24 * 0;
var d2 = (new Date(targetDayNum));
var dayNum = d2.getDay();
var dayName = ["Sun.","Mon.","Tues.","Wed.","Thurs.","Fri.","Sat."][dayNum];
var monthNum = d2.getMonth();
var monthName = ["Jan.","Feb.","March","April","May","June","July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."][monthNum];
var beginDay = dayName + ", " + monthName + " " + d2.getDate();
<!-- -->
<!-- gets last day of period -->
<!-- -->
var today = new Date();
var targetDay = new Date(today.getTime() + 6 * 24 * 60 * 60 * 1000);
var targetDayNum = today.valueOf() + 1000 * 60 * 60 * 24 * 6;
var d2 = (new Date(targetDayNum));
var dayNum = d2.getDay();
var dayName = ["Sun.","Mon.","Tues.","Wed.","Thurs.","Fri.","Sat."][dayNum];
var monthNum = d2.getMonth();
var monthName = ["Jan.","Feb.","March","April","May","June","July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."][monthNum];
var endDay = dayName + ", " + monthName + " " + d2.getDate();
document.write(beginDay + " - " + endDay);
</SCRIPT>
<!-- notes:
* To identify the begin and end period dates you only need to change the number of days in the targetDay and targetDayNum contructors. In this case "0" (today) and "6" (six days from today) were used.
* Add: ... + ", " + d2.getFullYear() in the beginDay or endDay constructor if you want the date to appear with the year as in:
Thurs., Jan. 6, 2011 - Wed., Jan. 12, 2011
-->


Please enter your Comment
  • Comment should be atleast 15 Characters.
  • Please put code inside [Code] your code [/Code].