Javascript 日期加 2 周(14 天)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7751936/
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
Javascript Date Plus 2 Weeks (14 days)
提问by jQuerybeast
I use this to get the date:
我用它来获取日期:
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
alert(month + "/" + day + "/" + year);
How can I add 2 weeks ? So instead of showing 10/13/2011, to show 10/27/2011 etc
我怎样才能增加 2 周?所以不是显示 10/13/2011,而是显示 10/27/2011 等
Here is the fiddle: http://jsfiddle.net/25wNa/
这是小提琴:http: //jsfiddle.net/25wNa/
I want the one input to have +14 days and the other +21
我希望一个输入有 +14 天,另一个输入有 +21 天
Note: I'd like the format to be > 10/13/2011 <.
注意:我希望格式为 > 10/13/2011 <。
回答by alex
12096e5
is a magic numberwhich is 14 days in milliseconds.
12096e5
是一个神奇的数字,以毫秒为单位是 14 天。
var fortnightAway = new Date(Date.now() + 12096e5);
回答by YuS
var currentTime = new Date();
currentTime.setDate(currentTime.getDate()+14);
回答by Pramod Pallath Vasudevan
have made a fidle for you http://jsfiddle.net/pramodpv/wfwuZ/
为你做了一个小提琴http://jsfiddle.net/pramodpv/wfwuZ/
Date.prototype.AddDays = function(noOfDays) {
this.setTime(this.getTime() + (noOfDays * (1000 * 60 * 60 * 24)));
return this;
}
Date.prototype.toString = function() {
return this.getMonth() + "/" + this.getDate() + "/" + this.getFullYear().toString().slice(2);
}
$(function() {
var currentTime = new Date();
alert(currentTime.AddDays(14));
});
回答by u993109
Try this:
尝试这个:
currentTime.setDate(currentTime.getDate()+14);
回答by Ognyan Dimitrov
12096e5
is a kind of magicnumber. Just 14 days in milliseconds in exponential notation.
12096e5
是一种神奇的数字。仅 14 天(以毫秒为单位)以指数表示法。
This number is the result of 1000[ms] * 60[s] * 60[m] * 24[h] * 14[d] saved in exponential notation.
这个数字是1000[ms] * 60[s] * 60[m] * 24[h] * 14[d] 以指数表示法保存的结果。
You can check it if you type Number('12096e5'). You will get 1209600000 [ms]which is exactly 2 weeks in milliseconds. The exponential notation makes it obscure.
如果您输入Number('12096e5') ,您可以检查它。您将获得1209600000 [ms],正好是 2 周(以毫秒为单位)。指数符号使它变得模糊。
You can write any other number in exponential notation to make the life of your fellow developers more interesting.
您可以用指数表示法编写任何其他数字,以使您的开发人员同事的生活更有趣。
Date objecthas constructor which accepts milliseconds as an argument which argument can be in exponential notation.
日期对象具有接受毫秒作为参数的构造函数,该参数可以是指数表示法。
var d = new Date(milliseconds);
var d = 新日期(毫秒);
var afterTwoWeeks = new Date(+new Date + 12096e5);
var afterTwoWeeks = new Date(+new Date + 1209600000);
Both are the same.
两者都是一样的。
回答by Spudley
Well, JS times are in millisecond, so adding two weeks would be a case of working out what two weeks is in milliseconds, and adding that value.
好吧,JS 时间以毫秒为单位,因此添加两周将是计算两周以毫秒为单位的情况,然后添加该值。
var twoWeeks = 1000 * 60 * 60 * 24 * 14;
var twoWeeksTime = new Date(new Date().getTime() + twoWeeks);
var formattedDate = twoWeeksTime.getDate() + '/' + (twoWeeksTime.getMonth()+1) + '/' + twoWeeksTime.getYear();
Of course, this method falls down if you need to add months, since they're variable in length, but it's fine for adding days and weeks.
当然,如果您需要添加月份,则此方法会失效,因为它们的长度是可变的,但是添加天数和周数也很好。
Alternatively, you use the DateJS library, which has functionality for exactly this kind of thing (plus loads more).
或者,您可以使用DateJS 库,它具有针对此类事情的功能(加上加载更多)。
With DateJS, your code could look like this:
使用 DateJS,您的代码可能如下所示:
var twoWeeksTime = Date.today().add({ days: 14 });
var formattedDate = twoWeeks.TimetoString('dd/MM/yy');
Hope that helps.
希望有帮助。
回答by Shlomi Komemi
add the following prototype method
添加以下原型方法
Date.prototype.addDays = function(days) {
this.setDate(this.getDate()+days);
}
and than its very simple to use,
而且使用起来非常简单,
currentTime.addDays(5);
回答by Narendra Yadala
If you are formatting a javascript date in a particular format, then I think you can have a look at this script http://blog.stevenlevithan.com/archives/date-time-format. All you would need to do after including the script is this new Date(+new Date + 1000* 60 * 60 * 24 * 14).format('dd/mm/yyyy')
and you would get the output "27/10/2011"
如果您以特定格式格式化 javascript 日期,那么我想您可以查看此脚本http://blog.stevenlevithan.com/archives/date-time-format。包含脚本后您需要做的就是这样new Date(+new Date + 1000* 60 * 60 * 24 * 14).format('dd/mm/yyyy')
,您将获得输出"27/10/2011"
The script is pretty small, just above 1KB minified. This is the link to a working fiddle http://jsfiddle.net/naryad/GufvT/
该脚本非常小,缩小到 1KB 以上。这是工作小提琴的链接http://jsfiddle.net/naryad/GufvT/