将今年添加到今天的日期 [英] Add year to todays date

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

问题描述

我想在今天的日期添加一年。我在一个不允许你使用标准 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);
// => 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)
// => 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天全站免登陆