如何获取日期选择器上的日期? [英] How to get the date on a datepicker?

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

问题描述

我真的是javascript和jquery的新手.我想获取用户通过datepicker选择的日期的值,并在我的JavaScript中使用该值.我的日期选择器的名称是birthDate.这是日期选择器的html代码:

I am really new to javascript and jquery. I wanted to get the value of the date selected by the user via datepicker, and use the value in my javascript. The name of my datepicker is birthDate. This is my html code for the date picker:

       <guis:datePicker required="true" label="Birthdate"
           bean="${accountInfo}" field="birthDate"
           value="${accountInfo?.birthDate?:"none"}"
           noSelection="${['null':'']}"/> 

我的JavaScript是:

My javascript is:

            <script type="text/javascript">                      
                function isOfLegalAge() {    
                  var currDate = new Date();
                  var birthDate = $("#datepicker").val();  
                  var diff = currDate - birthDate
                  alert (currDate);
                  alert(birthDate);                      
                      if(diff >= 18){
                          return true;                              
                      }else {                         
                          alert("You must be atleast 18 years old to register!");
                          document.location.href = '/user';
                          return false;                          
                      }
                      return true;
                }
           </script>

日期选择器返回的值采用以下格式:

The value returned by the datepicker is in this format:

1990年6月13日星期三00:00:00 PHT

Wed Jun 13 00:00:00 PHT 1990

..而新的日期是:

2012年6月6日星期三17:08:52 GMT + 0800(PHT)

Wed Jun 06 2012 17:08:52 GMT+0800 (PHT)

请帮助!

谢谢!

推荐答案

正如您所说的,您以dd/mm/yyyy格式获取日期试试

As you have said that you got the date in dd/mm/yyyy formatTry this,

var myDate = "06/06/2012";
var currentDate = new Date();
var compareDate = currentDate.getDate() + "/" + currentDate.getMonth() + "/" + currentDate.getFullYear();
if (myDate == compareDate)
    alert("Both dates are equal!");
else
    alert("Dates mismatch!");

如果还使用jQuery,则可以执行以下操作:

In case, if you are using jQuery also, you can do this:

var date = '06/06/2012';
var arrDate = date.split("/");
var today = new Date();
useDate = new Date(arrDate[2], arrDate[1] -1, arrDate[0]);

if (useDate == today)
    alert("Both dates are equal!");
else
    alert("Dates mismatch!");

希望这对您有所帮助!

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

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