[英]How to read Date from textBox, having Jquery UI DatePicker
The below Jquery UI Date Picker
shows 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 DateTime
it gives me error of Invalid Date
when every Day
is selected greater than 12
. 可以,但是当使用C#
作为字符串将其传递到服务器端,然后将其转换为DateTime
时,当每天选择的Day
大于12
时,它会给我Invalid Date
错误。 It treats with Day
as a month. 将Day
视为一个月。 And my required format of Date is dd/mm/yyyy
. 我所需的日期格式为dd/mm/yyyy
。
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, year to write your programs 您可以使用这3个可变的日,月,年来编写程序
Change var _myDate = new Date($('#date').val() );
更改var _myDate = new Date($('#date').val() );
to var _myDate = $('#date').val());
到var _myDate = $('#date').val());
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是一种动态类型的语言,因此不需要日期初始化
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());
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;
}
Just parse the textbox value using parseDate
with the same format you initially set up the datepicker: 只需使用parseDate
解析文本框值,其格式与您最初设置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对象,因此您无需手动创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.