javascript 如何在日期选择器上获取日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10910727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:27:17  来源:igfitidea点击:

How to get the date on a datepicker?

javascriptgrailsgroovydatepicker

提问by chemilleX3

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:

我对 javascript 和 jquery 真的很陌生。我想通过 datepicker 获取用户选择的日期的值,并在我的 javascript 中使用该值。我的日期选择器的名称是birthDate。这是我的日期选择器的 html 代码:

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

My javascript is:

我的 javascript 是:

            <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:

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

Wed Jun 13 00:00:00 PHT 1990

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

..while the new Date is:

..而新日期是:

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

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

Please help!

请帮忙!

Thanks!

谢谢!

回答by Praveen Kumar Purushothaman

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

正如你所说,你得到了dd/mm/yyyy格式的日期试试这个,

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!");

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

万一,如果您也使用 jQuery,您可以这样做:

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!");

Hope this helps you!

希望这对你有帮助!

回答by Amol Agase

try this

试试这个

<form:input cssStyle="background-image:url('../images/DateCal.png'); 
                               background-repeat:no-repeat; background-position: right;" 
                               path="requirementDate" id="datepicker1" 
                               />

  $(document).ready(function() {
      var dates= $( "#datepicker1" ).datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: '2012:2022'

        });

  });

回答by Jakub Oboza

if you have it in #datepickerid element you can use val to get value and turn it into Date like this:

如果您在#datepickerid 元素中有它,您可以使用 val 获取值并将其转换为 Date ,如下所示:

new Date( $('#datepicker').val() )

this will give you back Wed Jun 20 2012 00:00:00 GMT+0100 (BST):)

这会让你回来Wed Jun 20 2012 00:00:00 GMT+0100 (BST):)

Cheers!

干杯!

回答by Yaroslav

Try this:

试试这个:

var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();