Jquery / Javascript - 添加年份到日期变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14686243/
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 / Javascript - Add years to date variable
提问by Brandrally
I have a small trouble that would be great to have some help with. I am creating a small form that I want to take a current date formatted 'dd/mm/yyyy'and add a year(s) variable from a drop-down box to create a final expiry date. The only trouble is that I do not know how to parse the start-date as a date variable in order to complete the calculation. Any thoughts or help would be greatly appreciated. Paul.
我有一个小麻烦,希望得到一些帮助。我正在创建一个小表单,我想将当前日期格式化'dd/mm/yyyy'并从下拉框中添加一个年份变量以创建最终到期日期。唯一的麻烦是我不知道如何将开始日期解析为日期变量以完成计算。任何想法或帮助将不胜感激。保罗。
<script>
$(document).ready(function () {
$("#registerfor, #startdate, ").change(function () {
// Get Variables
var startdate = $("#startdate").val();
var registerfor = $("#registerfor").val();
// Add Years to Date
var expirydate = startdate + registerfor;
// Send Result on
$("#expires").val(expirydate);
});
});
</script>
<input name="startdate" id="startdate" value="dd/mm/yyyy" />
<select id="registerfor" name="registerfor">
<option value="1">1 Year</option>
<option value="2">2 Years</option>
<option value="3">3 Years</option>
</select>
采纳答案by Bruno
Sometimes what looks like quite a straight forward task becomes quite complicated.
有时看起来很简单的任务会变得很复杂。
$(document).ready( function () {
$("#registerfor, #startdate").change( function () {
var str = $("#startdate").val();
if( /^\d{2}\/\d{2}\/\d{4}$/i.test( str ) ) {
var parts = str.split("/");
var day = parts[0] && parseInt( parts[0], 10 );
var month = parts[1] && parseInt( parts[1], 10 );
var year = parts[2] && parseInt( parts[2], 10 );
var duration = parseInt( $("#registerfor").val(), 10);
if( day <= 31 && day >= 1 && month <= 12 && month >= 1 ) {
var expiryDate = new Date( year, month - 1, day );
expiryDate.setFullYear( expiryDate.getFullYear() + duration );
var day = ( '0' + expiryDate.getDate() ).slice( -2 );
var month = ( '0' + ( expiryDate.getMonth() + 1 ) ).slice( -2 );
var year = expiryDate.getFullYear();
$("#expires").val( day + "/" + month + "/" + year );
} else {
// display error message
}
}
});
});
Hereis a fiddle so you can see it in action.
这是一个小提琴,因此您可以看到它的实际效果。
回答by Christophe P
A quick and easy solution :
一个快速简便的解决方案:
var myDate = new Date();
myDate.setFullYear(myDate.getFullYear() + nbYearsToAdd);
"nbYearsToAdd" can be negative.
“nbYearsToAdd”可以是负数。
回答by mbharanidharan88
var dt = new Date($("#startdate").val());
var updateDate = dt.setDate(dt.getFullYear() + $("#registerfor").val());
var dd = updateDate.getDate();
var mm = updateDate.getMonth();
var y = updateDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
console.log(someFormattedDate);
回答by Sagar Hirapara
Dude, it's already working. If you still want to convert in date then do this:
伙计,它已经在工作了。如果您仍然想转换日期,请执行以下操作:
var dat=Date.parse(startdate );
回答by poomanchu
I would:
我会:
- put the date in a format that Date.parse can work nicely with (mm/dd/yyyy)
- add the number of milliseconds in a year times the selected option
Reformat the new value to a human-readable format and populate #expires
var dateArr = $("#startdate").val().split();var niceDate = Number(dateArr[1]) + "/" + Number(dateArr[0]) + "/" + Number(dateArr[2]);var expDate = Date.parse(niceDate) + 365*24*60*60*1000*Number($("#registerfor").val());$("#expires").val(expDate.getMonth()+1 + "/" + expDate.getDate() + "/" + expDate.getFullYear());
- 将日期放入 Date.parse 可以很好地使用的格式 (mm/dd/yyyy)
- 将一年中的毫秒数乘以所选选项
将新值重新格式化为人类可读的格式并填充 #expires
var dateArr = $("#startdate").val().split();var niceDate = Number(dateArr[1]) + "/" + Number(dateArr[0]) + "/" + Number(dateArr[2]);var expDate = Date.parse(niceDate) + 365*24*60*60*1000*Number($("#registerfor").val());$("#expires").val(expDate.getMonth()+1 + "/" + expDate.getDate() + "/" + expDate.getFullYear());
If you need 2-digit days and months for expiration date, you could have extra variables that hold a string version of them and check something like:var padMonth = expDate.getMonth()+1;if(expDate.getMonth()+1 < 10) padMonth = "0" + Number(expDate.getMonth()+1);
After which, you would use padMonth in $("#expires").val(...)
如果您需要 2 位数的日期和月份作为到期日期,您可以使用额外的变量来保存它们的字符串版本并检查如下内容:var padMonth = expDate.getMonth()+1;if(expDate.getMonth()+1 < 10) padMonth = "0" + Number(expDate.getMonth()+1);
之后,您将在 $("#expires").val(.. .)
回答by KAsh
lots of date picker jquery are available on the net
网上有很多日期选择器 jquery
one is my favourite, easy to implement and edit as per our needs.
一个是我最喜欢的,易于根据我们的需要实施和编辑。
if you want to use script from your code, you can directly pass the date value to var startdatelike
如果你想从你的代码中使用脚本,你可以直接将日期值传递给var startdate像
var startdate=<?php echo date("Y-m-d")?>
try this.. may help.
试试这个..可能会有所帮助。

