如何使用javascript从日期选择器获取日期 [英] how to get the day from date picker using javascript

查看:63
本文介绍了如何使用javascript从日期选择器获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript从日期选择器中获取当天



如果我今天选择日期,则需要在我的文本框中获取2012年2月20日星期一



我该怎么做?



[OP回答感动]

i想要得到这个,但它显示错误










今天=新日期()



//因为没有输入日期,所以它被视为今天。



//今天只是一个变量而不是关键字!



thisDay = today.getDay()



document.write(thisDay)

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)

推荐答案

试试,

在这里,先得到你的日期变量今天并使用方法.toDateString()覆盖它

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


嗨Priya



我假设您能够从中恢复该地址DatePicker但格式不是您想要的方式。例如,它在数字上显示为20120220(yyyymmdd)。所以这里有一段代码可以为你提供一天。



假设你的dateTime Picker以下列格式返回日期:

yyyymmdd。

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.
*****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 **********


抱歉重复我的任务再次ana,实际上我的问题是,我在使用javascipt的文本框中使用日期选择器,如果我选择今天的日期它显示日期(如21/02/2012)tat我从选择器中选择。但我需要和星期二21/02/2012一样的日子。可以帮助我吗?
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 ?


这篇关于如何使用javascript从日期选择器获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆