jQuery 添加 7 天至今(输入和输出类型文本)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17690278/
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
jQuery add 7 days to date (input and output type text)
提问by Gal Appelbaum
I am trying to do a simple page that takes a date (input type TEXT), and once the date is entered, another field will add 7 days to the input and display the date (+7 days) in a text input. My knowledge of jQuery is limited so I may have a small bug...
我正在尝试做一个带日期的简单页面(输入类型 TEXT),一旦输入日期,另一个字段将在输入中添加 7 天并在文本输入中显示日期(+7 天)。我对 jQuery 的了解有限,所以我可能有一个小错误......
<html>
<head>
<title>Date Plus 7 Days</title>
<script type="text/javascript">
$(document).ready(function(){
function DateFromString(str){
str = str.split(/\D+/);
str = new Date(str[2],str[0]-1,(parseInt(str[1])+7));
return MMDDYYYY(str);
}
function MMDDYYYY(str) {
var ndateArr = str.toString().split(' ');
var Months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec';
return (parseInt(Months.indexOf(ndateArr[1])/4)+1)+'/'+ndateArr[2]+'/'+ndateArr[3];
}
function Add7Days() {
var date = $('#start_date').val();
var ndate = DateFromString(date);
return ndate;
}
$('#start_date').change(function(){
$('#end_date') = Add7Days();
})
});
</script>
</head>
<body>
Start Date
<input type="text" id="start_date" value=''>
<br>
End date
<input type="text" id="end_date" value=''>
</body>
</html>
What did I do wrong?
我做错了什么?
Thanks!
谢谢!
回答by Kyle Muir
you've attempted to assign an object to $('#end_date')
. jQuery deals with this in a different way by altering the value of the input box by leveraging .val('value-here')
您试图将一个对象分配给$('#end_date')
. jQuery 通过利用改变输入框的值以不同的方式处理这个问题.val('value-here')
Try this:
尝试这个:
$('#start_date').change(function(){
$('#end_date').val(Add7Days());
});
See this fiddle: http://jsfiddle.net/zydZ2/
看到这个小提琴:http: //jsfiddle.net/zydZ2/
Also, Moment.JS is great for parsing and manipulating dates, I'd strongly recommend checking that out: http://momentjs.com/
此外,Moment.JS 非常适合解析和操作日期,我强烈建议您查看:http: //momentjs.com/
Hope this helps!
希望这可以帮助!
回答by Alok Agarwal
functionally to add 7days to an existing date can be achieved by using
在功能上可以通过使用来实现将 7 天添加到现有日期
var today_date = new Date()
alert(today_date)
today_date.setDate(today_date.getDate() + 7)
alert(today_date)
this will add seven days to an existing date if its 31 than it will make it 7th of next month
如果现有日期是 31 天,那么这将在现有日期的基础上增加 7 天,而不是下个月的 7 天
hope this help
希望这有帮助
回答by Yee Cheing Seah
Your may using JQuery & JqueryUI datepicker featured to did it.
您可能会使用 JQuery 和 JqueryUI 日期选择器来完成它。
$('#start_date').datepicker({
dateFormat: 'mm/dd/yy',
minDate: 0,
});
$("#end_date").datepicker({
dateFormat: 'mm/dd/yy',
minDate: 7,
});
var _dt = new Date();
var _dt = _dt.setDate(_dt.getDate());
$("#start_date").datepicker("setDate","mm/dd/yy", _dt);
$("#end_date").datepicker("setDate", "mm/dd/yy", _dt);
you may refer this http://jsfiddle.net/o9grLdf0/12/
你可以参考这个http://jsfiddle.net/o9grLdf0/12/
min = 0 for input startdate = today date and min = 7 mean today + 7 day for enddate
min = 0 表示输入 startdate = 今天日期, min = 7 表示今天 + 7 天结束日期
回答by William Wolface
function listDatesBetweenTwoDates(S_dateA, S_dateB) {
var O_dateA = moment(S_dateA).add(1, 'd');
var O_dateB = moment(S_dateB).add(1, 'd');
var O_itr = moment.twix(O_dateA, O_dateB).iterate("days");
var A_range = [];
while (O_itr.hasNext()) {
A_range.push(O_itr.next().toDate())
}
return A_range;
}