Javascript 日期加 2 周(14 天)

发布于 2024-12-09 11:58:52 字数 474 浏览 0 评论 0原文

我用它来获取日期:

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
alert(month + "/" + day + "/" + year);

如何添加 2 周?因此,不要显示 10/13/2011,而是显示 10/27/2011 等

这是小提琴: http://jsfiddle .net/25wNa/

我希望一个输入具有 +14 天,另一个输入具有 +21 天

注意:我希望格式为 > 2011 年 10 月 13 日 <.

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 <.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一笑百媚生 2024-12-16 11:58:52

12096e5 是一个 幻数,以毫秒为单位表示 14 天。

var fortnightAway = new Date(Date.now() + 12096e5);

jsFiddle

12096e5 is a magic number which is 14 days in milliseconds.

var fortnightAway = new Date(Date.now() + 12096e5);

jsFiddle.

神妖 2024-12-16 11:58:52
var currentTime = new Date();
currentTime.setDate(currentTime.getDate()+14);
var currentTime = new Date();
currentTime.setDate(currentTime.getDate()+14);
極樂鬼 2024-12-16 11:58:52

试试这个:

currentTime.setDate(currentTime.getDate()+14);

Try this:

currentTime.setDate(currentTime.getDate()+14);
゛清羽墨安 2024-12-16 11:58:52

已经为你制作了一把小提琴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));
    });

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));
    });
ゃ懵逼小萝莉 2024-12-16 11:58:52

12096e5一种神奇的数字。仅 14 天以指数表示法表示的毫秒

这个数字是1000[ms] * 60[s] * 60[m] * 24[h] * 14[d]以指数形式保存的结果。

如果您输入<强>数字('12096e5')。您将得到 1209600000 [ms],这正好是 2 周(以毫秒为单位)。指数符号使其变得模糊。

您可以用指数表示法写出任何其他数字,以使其他开发人员的生活更加有趣

Date 对象 具有接受毫秒作为参数的构造函数,该参数可以采用指数表示法。

var d = new Date(毫秒);

var afterTwoWeeks = new Date(+new Date + 12096e5);
var afterTwoWeeks = new Date(+new Date + 1209600000);

两者是相同的。

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.

野味少女 2024-12-16 11:58:52

嗯,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();

当然,如果您需要添加月份,则此方法会失败,因为它们的长度是可变的,但添加天和周就可以了。

或者,您可以使用 DateJS 库,它具有正是此类功能的功能(并且可以加载更多内容)。

使用 DateJS,您的代码可能如下所示:

var twoWeeksTime = Date.today().add({ days: 14 });
var formattedDate = twoWeeks.TimetoString('dd/MM/yy');

希望有帮助。

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.

陌上青苔 2024-12-16 11:58:52

从当前日期添加或减去 2 周

下面的代码示例以 YYYY-MM-DD 格式给出输出

如果在字符串中添加条件以将 0 与月份连接起来小于 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 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;
陌上芳菲 2024-12-16 11:58:52

添加以下原型方法

Date.prototype.addDays = function(days) {
     this.setDate(this.getDate()+days);
}

并且使用起来非常简单,

currentTime.addDays(5);

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);
一抹苦笑 2024-12-16 11:58:52

如果您要以特定格式格式化 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"

该脚本非常小,缩小后仅略高于 1KB。这是工作小提琴的链接 http://jsfiddle.net/naryad/GufvT/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文