最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get date of the next 15th in JavaScript - Stack Overflow

programmeradmin14浏览0评论

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

Examples:

  • So today 03.06.2015 would be 15.06.2015 23:59:59.
  • on 12.08.2015 it would be 15.08.2015 23:59:59
  • and on 18.02.2015 it would be 15.03.2015 23:59:59

I need the result in seconds.

I know how to do that in PHP, but failed to make it work with JavaScript.

Thanks a lot for helping!

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

Examples:

  • So today 03.06.2015 would be 15.06.2015 23:59:59.
  • on 12.08.2015 it would be 15.08.2015 23:59:59
  • and on 18.02.2015 it would be 15.03.2015 23:59:59

I need the result in seconds.

I know how to do that in PHP, but failed to make it work with JavaScript.

Thanks a lot for helping!

Share Improve this question edited Jun 3, 2015 at 6:09 user1693593 asked Jun 3, 2015 at 5:34 ArnieArnie 6819 silver badges20 bronze badges 4
  • 2 Are you sure about '16.08.2015'? It doesn't look like a 15th to me. :) – sainaen Commented Jun 3, 2015 at 5:37
  • The PHP code could be useful for people trying to answer. If it's not too much code, you could consider adding it to your question. – Peter Commented Jun 3, 2015 at 5:39
  • What did you try in JavaScript? – Bergi Commented Jun 3, 2015 at 5:39
  • I'm guessing it must be 01.08.2015 – Uma Kanth Commented Jun 3, 2015 at 5:39
Add a ment  | 

4 Answers 4

Reset to default 6

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)


If you want 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

Please try with 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 required more information.

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)

Given the string:

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

You can get the next 15th using a series 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 bine it all in 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 really depends on how much reuse you need from the functions.

发布评论

评论列表(0)

  1. 暂无评论