javascript 如何从文本框中读取日期,具有 Jquery UI DatePicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22196224/
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-27 22:40:08  来源:igfitidea点击:

How to read Date from textBox, having Jquery UI DatePicker

javascriptjquerydatepicker

提问by Shahid Iqbal

The below Jquery UI Date Pickershows the date in correct format, but reads it wrong.

下面Jquery UI Date Picker以正确的格式显示日期,但读错了。

HTML:

HTML:

<input type="text" id="date">
<input type="button" id="btn" value="Show"/>

JavaScript:

JavaScript:

$('#date').datepicker({dateFormat: 'dd-mm-yy'});
$('#btn').click(function(){
  var _myDate = new Date($('#date').val());
  alert(_myDate);
});

Whats wrong? ( Jsfiddle Here)

怎么了?(这里是 Jsfiddle

It shows the date correct, but reads it wrong...

它显示的日期正确,但读错了......

When I use it like this:

当我像这样使用它时:

$('#btn').click(function(){
      var _myDate = $('#date').val();
      alert(_myDate);
 });

It's ok, but when pass it to Server side using C#as string and then converting it to DateTimeit gives me error of Invalid Datewhen every Dayis selected greater than 12. It treats with Dayas a month. And my required format of Date is dd/mm/yyyy.

没关系,但是当使用C#作为字符串将其传递给服务器端然后将其转换为DateTime它时,会出现错误,Invalid Date即每个Day选择大于12. 将与Day视为一个月。我所需的日期格式是dd/mm/yyyy.

回答by Mohammad Kermani

maybe this is your answer

也许这就是你的答案

$( "#date" ).datepicker({dateFormat: 'mm-dd-yy'});
    $('#btn').click(function(){
    var _myDate = new Date($('#date').val());
    var new_date=_myDate.split('-');
    var month=new_date[0];
    var day=new_date[1];
    var year=new_date[2];
    //you have 3 data, month, day and year.

    alert(month+"/"+day+"/"+year);
    });

you can use that 3 variable day, month, yearto write your programs

您可以使用这 3 个可变的日、月、年来编写程序

回答by Shahid Iqbal

My javaScript function...

我的 JavaScript 函数...

convertDate = function (strDate) {
   var newDate = new Date();
   var oldDate = '';
   if (strDate.contains('/')) {
         oldDate = strDate.split('/');
      } else if (strDate.contains('-')) {
         oldDate = strDate.split('-');
      }
     //OldDate format is dd/mm/yyyy
      newDate.setDate(oldDate[0]);
      newDate.setMonth(oldDate[1] - 1);
      newDate.setYear(oldDate[2]);
      return newDate;
 }

回答by Delmo

Just parse the textbox value using parseDatewith the same format you initially set up the datepicker:

只需使用parseDate与您最初设置日期选择器相同的格式解析文本框值:

console.log($.datepicker.parseDate("dd-mm-yy", $('#date').val()));

parseDate will return a Date object so that you don't need to create it manually.

parseDate 将返回一个 Date 对象,因此您无需手动创建它。

回答by Toan Nguyen

Change var _myDate = new Date($('#date').val() );to var _myDate = $('#date').val());

_myDate = new Date($('#date').val() );将 var更改为 var_myDate = $('#date').val());

回答by Anoop Joshi

try like this

像这样尝试

$( "#date" ).datepicker({dateFormat: 'mm-dd-yy'});
$('#btn').click(function(){
var _myDate = new Date($('#date').val()  );
alert(_myDate);
});

DEMO

演示

回答by Carl Sargunar

You don't need the Date type declaration - you can just do

你不需要 Date 类型声明 - 你可以做

$( "#date" ).datepicker({dateFormat: 'dd-mm-yy'});
$('#btn').click(function(){
  var _myDate = $('#date').val();
alert(_myDate);
});

Javascript is a dynamically typed language so the Date initialisation is unnecessary

Javascript 是一种动态类型语言,因此不需要 Date 初始化

回答by Mr.G

Try this: it's work fine

试试这个:它工作正常

  $('#date').datepicker({dateFormat: 'dd-mm-yy',  changeMonth:true, changeYear:true,showOn: "both",buttonImage: "",buttonImageOnly: false });

console.log($('#date').val());