简体   繁体   English

如何将字符串转换为日期并在javascript中将该日期添加一年

[英]how to convert String to date and add one year to that date in javascript

I have this String 29/06/2017 i want to convert it into JavaScript date and add one year to it results Should be 28/06/2018 how to do it . 我有这个字符串29/06/2017我想将其转换为JavaScript日期并为其添加一年的结果应该是28/06/2018如何执行。 i have tried this 我已经试过了

var MyDate = new Date('29/06/2017');

Output is invalid date object how to convert it and add one year. 输出是invalid date object如何将其转换并增加一年。

Unlike the other users, i would like to let you know about another way to approach this issue. 与其他用户不同,我想让您了解解决此问题的另一种方法。

Moment.js Moment.js

Documentation - Reference 文档-参考

What's the purpose ? 目的是什么?

This library is very popular among Javascript Developer because it is easy to implement and allows you to manipulate Date way more easily with provided built-in features. 该库在Javascript Developer中非常受欢迎,因为它易于实现,并允许您使用提供的内置功能更轻松地操作Date方法。

Also, another interesting problem that is solved by using this library is the internationalization. 另外,使用此库解决的另一个有趣的问题是国际化。

Internationalization and Date 国际化和日期

As you may know, every language has its own way to handle and write date. 您可能知道,每种语言都有其自己的方式来处理和编写日期。 (Translated Month, Different Display, ect). (翻译月份,其他显示等)。

Moment.js provide a full suite of feature that allows you to deal with these easily with pre-translated words and syntax. Moment.js提供了一整套功能,使您可以使用预翻译的单词和语法轻松处理这些功能。

How to fix your issue with Moment.js 如何使用Moment.js解决问题

var myDate = moment("29/06/2017", "MM/DD/YYYY");

Furthermore you can check if your Date is valid 此外,您可以检查您的日期是否有效

myDate.isValid();

And here, you can simply add a year by using : 在这里,您只需使用以下命令即可添加一年:

myDate.add(1, 'y'); // Which stands for add a year

This is two questions on one. 这是两个有关的问题。 The parsing part is answered by Why does Date.parse give incorrect results . 为什么Date.parse会给出不正确的结果,回答了解析部分。 Once you have a Date, adding a year is straight forward but you need to account for adding a year to 29 February, which will return 1 March (since there will not be a 29 February the following year). 一旦有了日期,直接增加一年就可以了,但是您需要考虑在2月29日之前增加一年,这将返回3月1日(因为第二年将没有2月29日)。

 // Simple parser for DD/MM/YYYY function parseDMY(s) { var b = s.split(/\\D/); return new Date(b[2], b[1]-1, b[0]); } // Simple formatter: Date to DD/MM/YYYY function formatDMY(d) { return ('0' + d.getDate()).slice(-2) + '/' + ('0' + (d.getMonth()+1)).slice(-2) + '/' + d.getFullYear(); } // Add year to a Date function addYear(d) { var date = d.getDate(); d.setFullYear(d.getFullYear() + 1); // Test if 29 Feb rolled over to 1 Mar if (date != d.getDate()) { d.setDate(d.getDate() - 1); } return d; } // Tests (minimal) ['29/02/2016','29/6/2017'].forEach(function(s){ var date = parseDMY(s); addYear(date); console.log(formatDMY(date)); }); 

If you want the previous day (eg 30/06/2016 -> 29/06/2017) then you need to subtract a day after adding the year, which is the same as this line: 如果要前一天(例如30/06/2016-> 29/06/2017),则需要在添加年份后减去一天,这与以下行相同:

d.setDate(d.getDate() - 1);

or you can modify the addYear function to do it for you. 或者,您也可以修改addYear函数为您完成此操作。

You can try to switch the Day and the Year 您可以尝试切换日期和年份

var date = new Date('29/06/2017'.split('/').reverse().join('/'));
var plusOneYear = date.setFullYear(date.getFullYear()+1);
console.log(date); // output: Fri Jun 29 2018 00:00:00
console.log(new Date(plusOneYear)); // output: Fri Jun 29 2018 00:00:00
console.log(date.toLocaleDateString()) // output: 6/29/2018
console.log(date.getDate() + '/' + (date.getMonth+1) + '/' + date.getFullYear()); // output: 29/6/2018

another way is 另一种方法是

var date = '29/06/2017'.split('/'); // will become [day][month][year]
var newDate = date[2] + '/' + date[1] + '/' + date[0]; // join in format you would prefer
var plusOneYear = newDate.setFullYear(newDate.getFullYear()+1);
console.log(new Date(plusOneYear));
console.log(newDate.toLocaleDateString()) // output: 6/29/2018

Edited 已编辑

Try this, 尝试这个,

var MyDate = new Date('29/06/2017'.split('/').reverse().join('/')); 
new Date(MyDate.setYear(MyDate.getFullYear() + 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM