在今天的日期加上一年 [英] Add A Year To Today's Date

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

问题描述

我正在尝试在今天的日期前加上一年.我在不允许您使用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天全站免登陆