我需要使用以下格式的下一个 15 日的日期:15.06.2015 23:59:59
示例:
- 所以今天 03.06.2015 将是
15.06.2015 23:59:59
。 - 12.08.2015 将是
15.08.2015 23:59:59
- 在 18.02.2015 将是
15.03.2015 23:59:59
我需要几秒钟的结果。
我知道如何在 PHP 中做到这一点,但未能使其与 JavaScript 一起工作。
非常感谢您的帮助!
最佳答案
你可以尝试类似的东西
var date = new Date();
//if 15th of current month is over move to next month
//need to check whether to use >= or just > ie on 15th Jun
//if you want 15 Jun then use > else if you want 15 Jul use >=
var dt = date.getDate();
date.setDate(15);
if (dt >= 15) {
date.setMonth(date.getMonth() + 1);
}
date.setHours(23, 59, 59, 0);
document.write(date)
如果你想要一个返回新日期对象的函数
function setNext15(date) {
var next = new Date(date);
var cache = next.getDate();
next.setDate(15);
if (cache >= 15) {
next.setMonth(date.getMonth() + 1);
}
next.setHours(23, 59, 59, 0);
console.log(next, date);
return next;
}
演示:Fiddle
关于javascript - 在 JavaScript 中获取下一个 15 号的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30611586/