至今添加30天(mm / dd / yy) [英] Add 30 days to date (mm/dd/yy)

查看:90
本文介绍了至今添加30天(mm / dd / yy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式(mm / dd / yy),并希望添加30天。我只是好奇这样做的最好方法?我是新的javascript,所以例子会很有帮助。

I have a date in the format (mm/dd/yy) and want to add 30 days to it. I am just curious the best method to do this? I am new to javascript so examples would be helpful.

EDITED

对不起我正在使用美国的日期格式(mm / dd / yy)。

Sorry I am using US date format (mm/dd/yy).

推荐答案

只需添加30天到今天:

Simply add 30 days to todays date:

var now = new Date();
now.setDate(now.getDate() + 30);

但是,你真的想做什么?或者您想要获得今天加上一个月?

However, is that what you really want to do? Or do you want to get today plus one month?

您可以使用以下方式将广告/ m / y日期转换为日期对象:

You can convert a d/m/y date to a date object using:

var dString = '9/5/2011';
var dParts = dString.split('/');
var in30Days = new Date(dParts[2] + '/' +
                        dParts[1] + '/' +
                        (+dParts[0] + 30)
               );

对于美国的日期格式,交换零件0和1:

For US date format, swap parts 0 and 1:

var in30Days = new Date(dParts[2] + '/' +
                        dParts[0] + '/' +
                        (+dParts[1] + 30)
               );

但是,在给予该功能之前,将日期更新为ISO8601格式,您真的不应该在同一功能中混合日期解析和算术。一个全面的日期解析函数是复杂的(不是过分的,但是它们很繁琐,需要大量的测试),一旦你有一个日期对象,算术就很简单。

But it is better to get the date into an ISO8601 format before giving it to the function, you really shouldn't be mixing date parsing and arithmetic in the same function. A comprehensive date parsing function is complex (not excessively but they are tediously long and need lots of testing), arithmetic is quite simple once you have a date object.

这篇关于至今添加30天(mm / dd / yy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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