将一年添加到今天的日期 [英] Add A Year To Today's Date

查看:34
本文介绍了将一年添加到今天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为今天的日期添加一年.我在一个不允许您使用 standard JavaScript 的系统中工作.

I am trying to add a year to todays date. I am working in a system that does not allow you to use standard JavaScript.

例如,要获取今天的日期,我必须使用:

For instance, to get todays date I have to use:

javascript:now();

我试过了:

javascript:now(+1);

我以前从未见过这个,但我需要在今天的日期上加一年...

I have never seen this before, but am in need of adding one year to todays date...

以前有没有人见过这种方式获取当前日期?如果是这样,我怎么能增加一年?

Has anyone seen getting current date this way before? And if so, how could I add a year?

推荐答案

您可以使用以下代码创建一个带有今天日期的新日期对象:

You can create a new date object with todays date using the following code:

var d = new Date();
    console.log(d);

//=> 2015 年 10 月 11 日星期日 14:46:51 GMT-0700 (PDT)

// => Sun Oct 11 2015 14:46:51 GMT-0700 (PDT)

如果要在特定时间创建日期,可以传递新的 Date 构造函数参数

If you want to create a date a specific time, you can pass the new Date constructor arguments

 var d = new Date(2014);
    console.log(d)

// => Wed Dec 31 1969 16:00:02 GMT-0800 (PST)

如果要取今天的日期加上年份,可以先创建一个日期对象,访问相关的属性,然后使用它们来创建一个新的日期对象

If you want to take todays date and add a year, you can first create a date object, access the relevant properties, and then use them to create a new date object

var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();
    var c = new Date(year + 1, month, day);
    console.log(c);

// => Tue Oct 11 2016 00:00:00 GMT-0700 (PDT)

你可以在 MDN 上阅读更多关于日期对象的方法

You can read more about the methods on the date object on MDN

日期对象

这篇关于将一年添加到今天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆