开发者

Javascript Date Plus 2 Weeks (14 days)

开发者 https://www.devze.com 2023-04-12 22:26 出处:网络
I use this to get the date: var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate()

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 week开发者_开发知识库s ? 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 <.


12096e5 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/

0

精彩评论

暂无评论...
验证码 换一张
取 消