Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to get the day from date picker using javascript

if i select today date, it need to get in my textbox lik Feb 20 monday 2012

how can i do this ?

[OP answer moved]
i want t get lik this but it shows error





today=new Date()

//as no date has been entered, then it is taken as today.

//today is just a variable and is not a keyword!

thisDay=today.getDay()

document.write(thisDay)
Posted
Updated 20-Feb-12 4:28am
v2
Comments
The Zetta 20-Feb-12 9:26am    
What datepicker?! are we speaking .NET datepicker? or third party?
priya from Madras 20-Feb-12 9:38am    
k how to find the day from the selected date
Sergey Alexandrovich Kryukov 20-Feb-12 10:03am    
Please don't repeat your question but answer. What DateTimePicker, exactly? Would you just look at it by yourself?
--SA
priya from Madras 21-Feb-12 1:56am    
sorry for repeating my quest again ,actually my question is ,am using date picker in a textbox using javascipt , if i select the date of today it shows the date(Like 21/02/2012) tat what i ve select from that picker .but i need to get with day like 21/02/2012 tuesday .can any one help me ?

try like,
Here, first get your date in variable "today" and use the method .toDateString() over it
C#
var today = new Date();
var readable = today.toDateString();
//alert(readable);
document.writeln(readable);
 
Share this answer
 
Hi Priya

I'm assuming that you are able to recover the address from the DatePicker but the format is not the way you want it. For example it is displayed numerically like 20120220 (yyyymmdd). So here is a piece of code that will provide you with the day.

Assuming your dateTime Picker returns the date in the following format:
yyyymmdd.
JavaScript
*****CODE START**********
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var iMonthStart = [0,3,3,6,1,4,6,2,5,0,3,5,6,2];

var date = myDatePicker.selectedDate(); // Not sure what kind of datePicker you are using but you call the method that returns the selected date.
// 01234567
// yyyymmdd
var century = date.substring(0,2); // Century
var year = date.substring(2,2); //Year
var month = date.substring(4,2);
var day    = date.substring(6,2);

//Please refer to the algorithm described here: <a href="http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week">http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week</a>[<a href="http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week" target="_blank" title="New Window">^</a>]

var centuryForCalc = 2*(3-(century%4));
var yearForCalc = year/4;

//Test for leap year
var yearFull = date.substring(0,4);
var iModLeap = yearFull%4;
var monthTblVal = 0;
if(iModLeap == 0)
{
   // Leap Year
   if(month == 1)
   {
       monthTblVal = 6;
    }
    else if(month == 2)
    {
      monthTblVal = 2;
    }
    else
    {
      monthTblVal = iMonthStart[month]; 
    }
}
else
{
   monthTblVal = iMonthStart[month]; 
}

var iTotal = centuryForCalc + yearForCalc + year + monthTblVal + day;
var iDayIndex = iTotal%7;
var dayString = days[iDayIndex-1];
*********** CODE END **********
 
Share this answer
 
Comments
fjdiewornncalwe 20-Feb-12 12:16pm    
Just added pre tags to your answer. It is recommended that all code blocks be done this way at CP. Cheers.
sorry for repeating my quest again ana again ,actually my question is ,am using date picker in a textbox using javascipt , if i select the date of today it shows the date(Like 21/02/2012) tat what i ve select from that picker .but i need to get with day like 21/02/2012 tuesday .can any one help me ?
 
Share this answer
 
Your question is not clear. I'm assuming you are using a .NET DatePicker object. in that case just do like this :

DateTime selected = datetimepicker.Value;
int day = selected.day;
string dayofweek = selected.dayofweek;



---------------------
Regards

H.Maadani
 
Share this answer
 
Comments
fjdiewornncalwe 20-Feb-12 10:35am    
My vote of 1: The OP has clearly stated Javascript so code behind is not an option.
The Zetta 20-Feb-12 10:37am    
you can not access ASP.NET objects and their properties by javascript! your best shot is getting date by Ajax, and manipulating it by javascript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900