使用 javascript 添加日期至今
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6368153/
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
Add days to date using javascript
提问by Tassadaque
I am trying to add days to a given date using javascript. I have the following code
我正在尝试使用 javascript 为给定日期添加天数。我有以下代码
function onChange(e) {
var datepicker = $("#DatePicker").val();
alert(datepicker);
var joindate = new Date(datepicker);
alert(joindate);
var numberOfDaysToAdd = 1;
joindate.setDate(joindate + numberOfDaysToAdd);
var dd = joindate.getDate();
var mm = joindate.getMonth() + 1;
var y = joindate.getFullYear();
var joinFormattedDate = dd + '/' + mm + '/' + y;
$('.new').val(joinFormattedDate);
}
On first alert i get the date 24/06/2011
but on second alert i get Thu Dec 06 2012 00:00:00 GMT+0500 (Pakistan Standard Time)
which is wrong i want it to remain 24/06/2011
so that i can add days to it. In my code i want my final output to be 25/06/2011
在第一次警报时,我得到了日期,24/06/2011
但在第二次警报时,我知道Thu Dec 06 2012 00:00:00 GMT+0500 (Pakistan Standard Time)
哪个是错误的,我希望它保留下来,24/06/2011
以便我可以添加天数。在我的代码中,我希望我的最终输出是25/06/2011
Fiddle is @ http://jsfiddle.net/tassadaque/rEe4v/
回答by Salman A
Date('string')
will attempt to parse the string as m/d/yyyy
. The string 24/06/2011
thus becomes Dec 6, 2012
. Reason: 24 is treated as a month... 1 => January 2011, 13 => January 2012 hence 24 => December 2012. I hope you understand what I mean. So:
Date('string')
将尝试将字符串解析为m/d/yyyy
. 字符串24/06/2011
因此变为Dec 6, 2012
。原因:24 被视为一个月... 1 => 2011 年 1 月,13 => 2012 年 1 月,因此 24 => 2012 年 12 月。我希望你明白我的意思。所以:
var dmy = "24/06/2011".split("/"); // "24/06/2011" should be pulled from $("#DatePicker").val() instead
var joindate = new Date(
parseInt(dmy[2], 10),
parseInt(dmy[1], 10) - 1,
parseInt(dmy[0], 10)
);
alert(joindate); // Fri Jun 24 2011 00:00:00 GMT+0500 (West Asia Standard Time)
joindate.setDate(joindate.getDate() + 1); // substitute 1 with actual number of days to add
alert(joindate); // Sat Jun 25 2011 00:00:00 GMT+0500 (West Asia Standard Time)
alert(
("0" + joindate.getDate()).slice(-2) + "/" +
("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +
joindate.getFullYear()
);
回答by Dhruva Sagar
I would like to encourage you to use DateJSlibrary. It is really awesome!
我想鼓励你使用DateJS库。真是太棒了!
function onChange(e) {
var date = Date.parse($("#DatePicker").val()); //You might want to tweak this to as per your needs.
var new_date = date.add(n).days();
$('.new').val(new_date.toString('M/d/yyyy'); //You might want to tweak this as per your needs as well.
}
回答by RobG
Assuming numberOfDaysToAddis a number:
假设numberOfDaysToAdd是一个数字:
joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
回答by mplungjan
The first alert is the value of the field. the second is the generated date from a non-US formatted date.
第一个警报是该字段的值。第二个是从非美国格式的日期生成的日期。
Here is a working example
这是一个工作示例
(似乎这种标记是引起注意的必要条件)If you want to keep your code, then you need to change
如果你想保留你的代码,那么你需要改变
var joindate = new Date(datepicker);
var joindate = new Date(datepicker);
to
到
var parms = datepicker.split("/");
then use
然后使用
var joindate = new Date(parms[1]+"/"+parms[0]+"/"+parms[2]);
OR the identically working
或相同的工作
var joindate = new Date(parms[2],parms[1]-1,parms[0]);
As pointed out in a few other answers too, use the .getDate()
正如其他一些答案中所指出的,使用 .getDate()
joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
Lastly you want to add a 0 if the month is < 10
最后,如果月份 < 10,您想添加 0
if (mm<10) mm="0"+mm;
If you are using the datepicker from jQuery UI, then you can do
如果您使用的是 jQuery UI 中的日期选择器,那么您可以这样做
$('.new').val($("#DatePicker").datepicker( "setDate" , +1 ).val());
$('.new').val($("#DatePicker").datepicker( "setDate" , +1 ).val());
instead of your function
而不是你的功能
http://jqueryui.com/demos/datepicker/#method-setDate
http://jqueryui.com/demos/datepicker/#method-setDate
Sets the current date for the datepicker. The new date may be a Date object or a string in the current date format (e.g. '01/26/2009'), a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null to clear the selected date.
设置日期选择器的当前日期。新日期可以是 Date 对象或当前日期格式的字符串(例如 '01/26/2009')、距今天的天数(例如 +7)或一串值和句点('y' 表示年,'m' 表示月,'w' 表示周,'d' 表示天,例如 '+1m +7d'),或 null 以清除所选日期。
回答by Gedrox
Try
尝试
function onChange(e) {
var datepicker = $("#DatePicker").val();
alert(datepicker);
var parts = datepicker.split(/[^\d]/);
var joindate = new Date();
joindate.setFullYear(parts[2], parts[1]-1, parts[0]);
alert(joindate);
var numberOfDaysToAdd = 1;
joindate.setDate(joindate + numberOfDaysToAdd);
var dd = joindate.getDate();
var mm = joindate.getMonth() + 1;
var y = joindate.getFullYear();
var joinFormattedDate = dd + '/' + mm + '/' + y;
$('.new').val(joinFormattedDate);
}
I suppose the problem is JavaScript expects format MM/DD/YYYY not DD/MM/YYYY when passed into Date constructor.
我想问题是 JavaScript 在传入 Date 构造函数时期望格式为 MM/DD/YYYY 而不是 DD/MM/YYYY。
回答by Alnitak
To answer your real problem, I think your issue is that you're trying to parse the text-value of the DatePicker, when that's not in the right format for your locale.
为了回答您的真正问题,我认为您的问题是您正在尝试解析 DatePicker 的文本值,但它的格式不适合您的语言环境。
Instead of .val()
, use:
代替.val()
,使用:
var joindate = $('#DatePicker').datepicker("getDate");
to get the underyling Date()
object representing the selected date directly from jQuery.
Date()
直接从 jQuery获取代表所选日期的底层对象。
This guarantees that the date object is correct regardless of the date format specified in the DatePicker
or the current locale.
这保证了日期对象是正确的,而不管在DatePicker
或当前语言环境中指定的日期格式如何。
Then use:
然后使用:
joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
to move it on.
继续前进。
回答by Liangliang Zheng
Is it a typo round joindate.setDate(joindate + numberOfDaysToAdd)?
joindate.setDate(joindate + numberOfDaysToAdd) 是不是打字错误?
I tried this code, it seems ok to me
我试过这段代码,对我来说似乎没问题
var joindate = new Date(2010, 5, 24);
alert(joindate);
var numberOfDaysToAdd = 1;
joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
var dd = joindate.getDate();
var mm = joindate.getMonth() + 1;
var y = joindate.getFullYear();
var joinFormattedDate = dd + '/' + mm + '/' + y;
alert(joinFormattedDate);
回答by Beena Shetty
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};
and in your javascript code you could call
在你的 javascript 代码中你可以调用
var currentDate = new Date();
// to add 8 days to current date
currentDate.addDays(8);
回答by roberkules
function onChange(e) {
var datepicker = $("#DatePicker").val().split("/");
var joindate = new Date();
var numberOfDaysToAdd = 1;
joindate.setFullYear(parseInt(datepicker[2]), parseInt(datepicker[1])-1, parseInt(datepicker[0])+numberOfDaysToAdd);
$('.new').val(joindate);
}
回答by TurKux
try this.
尝试这个。
Date.prototype.addDay = function(numberOfDaysToAdd){
this.setTime(this.getTime() + (numberOfDaysToAdd * 86400000));
};
function onChange(e) {
var date = new Date(Date.parse($("#DatePicker").val()));
date.addDay(1);
var dd = date.getDate();
var mm = date.getMonth() + 1;
var y = date.getFullYear();
var joinFormattedDate = dd + '/' + mm + '/' + y;
$('.new').val(joinFormattedDate);
}