admin管理员组文章数量:1134550
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
Here is the fiddle: /
I want the one input to have +14 days and the other +21
Note: I'd like the format to be > 10/13/2011 <.
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
Here is the fiddle: http://jsfiddle.net/25wNa/
I want the one input to have +14 days and the other +21
Note: I'd like the format to be > 10/13/2011 <.
Share Improve this question edited Sep 26, 2015 at 3:31 alex 490k204 gold badges889 silver badges990 bronze badges asked Oct 13, 2011 at 9:19 jQuerybeastjQuerybeast 14.5k39 gold badges119 silver badges198 bronze badges 1- 1 Please, use the ISO date format: YYYY-MM-DD xkcd.com/1179 – Artur Czajka Commented May 6, 2013 at 13:52
9 Answers
Reset to default 12512096e5
is a magic number which is 14 days in milliseconds.
var fortnightAway = new Date(Date.now() + 12096e5);
jsFiddle.
var currentTime = new Date();
currentTime.setDate(currentTime.getDate()+14);
Try this:
currentTime.setDate(currentTime.getDate()+14);
have made a fidle for you 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));
});
12096e5
is a kind of magic number. Just 14 days in milliseconds in exponential notation.
This number is the result of 1000[ms] * 60[s] * 60[m] * 24[h] * 14[d] saved in exponential notation.
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.
You can write any other number in exponential notation to make the life of your fellow developers more interesting.
Date object has constructor which accepts milliseconds as an argument which argument can be in exponential notation.
var d = new Date(milliseconds);
var afterTwoWeeks = new Date(+new Date + 12096e5);
var afterTwoWeeks = new Date(+new Date + 1209600000);
Both are the same.
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.
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).
With DateJS, your code could look like this:
var twoWeeksTime = Date.today().add({ days: 14 });
var formattedDate = twoWeeks.TimetoString('dd/MM/yy');
Hope that helps.
Add or Subtract 2 weeks from current date
Below code example give output in YYYY-MM-DD format
If condition is added in the string to concatenate 0 with Month and Day which is less than 10.
var twoWeeks = 1000 * 60 * 60 * 24 * 14;
var twoWeeksTime = new Date(new Date().getTime() + twoWeeks); /* Add or Subtract here*/
var formattedDate = (twoWeeksTime.getFullYear()) + '-' +
((twoWeeksTime.getMonth()+1) < 10 ? "0"+(twoWeeksTime.getMonth()+1): (twoWeeksTime.getMonth()+1)) + '-' +
(twoWeeksTime.getDate() < 10 ? "0"+(twoWeeksTime.getDate()): (twoWeeksTime.getDate()));
document.body.innerHTML = formattedDate;
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);
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"
The script is pretty small, just above 1KB minified. This is the link to a working fiddle http://jsfiddle.net/naryad/GufvT/
本文标签: Javascript Date Plus 2 Weeks (14 days)Stack Overflow
版权声明:本文标题:Javascript Date Plus 2 Weeks (14 days) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736893872a1955992.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论