javascript 如何获取日期输入字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23971870/
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
how can I get the value of a date input field
提问by user2828251
I'm using an HTML input field of a date:
我正在使用日期的 HTML 输入字段:
<input id="datepicker" type="date" value="11/12/2013" />
How can I get using JQuery the different values for the year, month and day? each one need to be save as a different var
如何使用 JQuery 获取年、月和日的不同值?每个都需要保存为不同的 var
Thanks,
谢谢,
回答by kemicofa ghost
EDIT: For this to work you need to change type="text" OR you look at this post: set date in input type date
编辑:为此,您需要更改 type="text" 或者您查看这篇文章:在输入类型日期中设置日期
var d = $('#datepicker').val().split('/');
var year = d[2];
var month = d[1];
var day = d[0];
Supposing you are using the DD/MM/YYYY format.
假设您使用的是 DD/MM/YYYY 格式。
回答by Milind Anantwar
You can use split by /
to get them in array individually:
您可以使用 split by/
将它们单独放入数组中:
var date=$('#datepicker').val().split('/');
var day=date[0];
var month=date[1];
var year=date[2];
回答by Ibrahim Khan
try like this:
试试这样:
Html:
网址:
<input id="datepicker" type="date" value="2013-11-11" />
JavaScript:
JavaScript:
var date = $('#datepicker').val().split('-');
var year = date[2];
var month = date[1];
var day = date[0];
Note: type date take value in yyyy-mm-dd format
注意:输入日期取值 yyyy-mm-dd 格式
回答by Leon
With Javascript es6 you can do a one-liner
使用 Javascript es6 你可以做一个单行
let [day,month,year] = $('#datepicker').val().split('-');
let [day,month,year] = $('#datepicker').val().split('-');