在JavaScript中获取下一个15日的日期

Arn*_*nie 3 javascript date

我需要使用以下格式的下一个15日期: 15.06.2015 23:59:59

例子:

  • 所以今天03.06.2015将是15.06.2015 23:59:59.
  • 2015年8月12日这将是15.08.2015 23:59:59
  • 并且在2015年2月18日它将是15.03.2015 23:59:59

我需要几秒钟的结果.

我知道如何在PHP中执行此操作,但未能使其与JavaScript一起使用.

非常感谢您的帮助!

Aru*_*hny 6

你可以尝试类似的东西

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)
Run Code Online (Sandbox Code Playgroud)


如果需要一个返回新日期对象的函数

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;
}
Run Code Online (Sandbox Code Playgroud)

演示:小提琴