Get date of next 15th in JavaScript

I need a date on the next 15th using this format: 15.06.2015 23:59:59

<strong> Examples:

  • So, today 06/03/2015 will be 15.06.2015 23:59:59

    .
  • 12.08.2015 it will be 15.08.2015 23:59:59

  • and 18.02.2015 it will be 15.03.2015 23:59:59

I need the result in seconds.

I know how to do this in PHP, but could not get it to work with JavaScript.

Many thanks for the help!

+3


source to share


4 answers


You can try something like

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 codeHide result





If you need a function that returns a new date object

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;
}

      

Demo: Fiddle

+6


source


Try using the below code snippet.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var d1 = new Date();
if(d1.getDate() > 15)
{
  d1.setDate(d1.getDate() + 17);
}
var d2 = new Date(d1.getFullYear(),d1.getMonth(),15,23,59,59,0);
document.getElementById("demo").innerHTML = d2.getDate()+"."+d2.getMonth()+ "." + d2.getFullYear()+ " " + d2.getHours()+":"+ d2.getMinutes() + ":" + d2.getSeconds();

</script>

</body>
</html>

      



Let me know if you need more information.

+2


source


function next15th(d) {
    // prevent mutation of original date
    var copy = new Date(d.getTime());
    if (copy.getDate() >= 15) {
        // not in the same month
        copy.setMonth(copy.getMonth() + 1);
    }
    copy.setDate(15);
    copy.setHour(23, 59, 59);
    return copy;
}

var d = new Date();
console.log(d);
//> Wed Jun 03 2015 09:04:24 GMT+0300 (EEST)
console.log(next15th(d));
//> Mon Jun 15 2015 23:59:59 GMT+0300 (EEST)
console.log(d);
//> Wed Jun 03 2015 09:04:24 GMT+0300 (EEST)

      

0


source


Given the line:

var dateString = '15.06.2015 23:59:59';

      

You can get the next 15th using a number of small functions:

// Parse string in format d/m/y h:m:s
function parseDMY(s) {
  var b = s.split(/\D/);
  return new Date(b[2], b[1]-1, b[0], b[3], b[4], b[5]);
}

// Format date as dd.mm.yyyy hh:mm:ss
function formatDMY(date) {
  function z(n){return (n<10?'0':'') + n}
  return z(date.getDate()) + '.' + z(date.getMonth()+1) + '.' + date.getFullYear() + ' ' +
         z(date.getHours()) + ':' + z(date.getMinutes()) + ':' + z(date.getSeconds());
}

// Set to next 15th
function getNext15th(date) {
  if (date.getDate() >= 15) {
    date.setMonth(date.getMonth() + 1);
  }
  date.setDate(15);
  return date;
}

// Calling each sequentially 
console.log(formatDMY(getNext15th(parseDMY(dateString)))); // 15.07.2015 23:59:59

      

Or, you can combine it all into one function:

function getNext15th(s) {
  function z(n){return (n<10?'0':'') + n}
  var b = s.split(/\D/);
  var date = new Date(b[2], b[1] - (b[0] >= 15? 0 : 1), 15, b[3], b[4], b[5]);
  return z(date.getDate()) + '.' + z(date.getMonth()+1) + '.' + date.getFullYear() + ' ' +
         z(date.getHours()) + ':' + z(date.getMinutes()) + ':' + z(date.getSeconds());
}

console.log(getNext15th(dateString)); // 15.07.2015 23:59:59
console.log(getNext15th('15.012.2015 23:59:59')); // 15.01.2016 23:59:59

      

It depends on how much reuse you want from functions.

0


source







All Articles