我有格式(mm/dd/yy)的日期,并希望添加30天.我只是好奇最好的方法吗?我是javascript的新手,所以示例会有所帮助.
EDITED
对不起,我使用美国日期格式(mm/dd/yy).
Rob*_*obG 38
向日期字符串添加30天的一种方法是将其解析为日期,添加30天,然后将其格式化为字符串.
应使用定制函数或库手动解析日期字符串.无论哪种方式,您需要知道格式以了解它是否已被正确解析,例如
// Given a string in m/d/y format, return a Date
function parseMDY(s) {
var b = s.split(/\D/);
return new Date(b[2], b[0]-1, b[1]);
}
// Given a Date, return a string in m/d/y format
function formatMDY(d) {
function z(n){return (n<10?'0':'')+n}
if (isNaN(+d)) return d.toString();
return z(d.getMonth()+1) + '/' + z(d.getDate()) + '/' + d.getFullYear();
}
// Given a string in m/d/y format, return a string in the same format with n days added
function addDays(s, days) {
var d = parseMDY(s);
d.setDate(d.getDate() + Number(days));
return formatMDY(d);
}
[['6/30/2018', 30],
['1/30/2018', 30], // Goes from 30 Jan to 1 Mar
['12/31/2019', 30]].forEach(a => {
console.log(`${a[0]} => ${addDays(...a)}`);
});Run Code Online (Sandbox Code Playgroud)
如果"30天"标准被解释为添加一个月,那就有点棘手了.1月31日增加1个月将于2月31日到3月2日或3日,具体取决于该年2月是28天还是29天.解决这个问题的一种算法是查看月份是否已经过远并将日期设置为上个月的最后一天,因此2018-01-31加上一个月给出了2018-02-28.
相同的算法适用于减去月份,例如
/**
* @param {Date} date - date to add months to
* @param {number} months - months to add
* @returns {Date}
*/
function addMonths(date, months) {
// Deal with invalid Date
if (isNaN(+date)) return;
months = parseInt(months);
// Deal with months not being a number
if (isNaN(months)) return;
// Store date's current month
var m = date.getMonth();
date.setMonth(date.getMonth() + months);
// Check new month, if rolled over an extra month,
// go back to last day of previous month
if (date.getMonth() != (m + 12 + months)%12) {
date.setDate(0);
}
// date is modified in place, but return for convenience
return date;
}
// Helper to format the date as D-MMM-YYYY
// using browser default language
function formatDMMMY(date) {
var month = date.toLocaleString(undefined,{month:'short'});
return date.getDate() + '-' + month + '-' + date.getFullYear();
}
// Some tests
[[new Date(2018,0,31), 1],
[new Date(2017,11,31), 2],
[new Date(2018,2,31), -1],
[new Date(2018,6,31), -1],
[new Date(2018,6,31), -17]].forEach(a => {
let f = formatDMMMY;
console.log(`${f(a[0])} plus ${a[1]} months: ${f(addMonths(...a))}`);
});Run Code Online (Sandbox Code Playgroud)
当然,库可以帮助解决上述问题,算法是相同的.
只需在今天的日期添加30天:
var now = new Date();
now.setDate(now.getDate() + 30);
Run Code Online (Sandbox Code Playgroud)
但是,你真的想做什么?或者你想今天加一个月?
您可以使用以下方法将广告/月份日期转换为日期对象:
var dString = '9/5/2011';
var dParts = dString.split('/');
var in30Days = new Date(dParts[2] + '/' +
dParts[1] + '/' +
(+dParts[0] + 30)
);
Run Code Online (Sandbox Code Playgroud)
对于美国日期格式,交换零件0和1:
var in30Days = new Date(dParts[2] + '/' +
dParts[0] + '/' +
(+dParts[1] + 30)
);
Run Code Online (Sandbox Code Playgroud)
但是在将日期转换为函数之前,最好将日期转换为ISO8601格式,您实际上不应该在同一函数中混合日期解析和算术.全面的日期解析功能很复杂(不过分,但它们冗长且需要大量测试),一旦有了日期对象,算术就很简单了.
小智 14
完成它的一个简单方法是在Date构造函数中发送timestamp值.计算以时间戳衡量的30天:
30 * 24 * 60 * 60 * 1000
然后,您需要当前时间戳:
Date.now()
最后,将两个值相加并将结果作为构造函数中的参数发送:
var nowPlus30Days = new Date(Date.now() + (30 * 24 * 60 * 60 * 1000));
| 归档时间: |
|
| 查看次数: |
37045 次 |
| 最近记录: |