M
M
Masterocek2017-05-23 10:13:17
JavaScript
Masterocek, 2017-05-23 10:13:17

How to make automatic date change js?

The task is to make an automatic date change. The date should change every 15 days, for example, from 1-15 there is one date, from 15-30 another, but it doesn’t work out. Here's a little bit of code:

<!-- clock hack -->
<!DOCTYPE html>
<html lang="ru">
<head>
<title>Пример</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript">


var dt = new Date();

// Display the month, day, and year. getMonth() returns a 0-based number.
var day = dt.getDate()+5;
var month = dt.getMonth()+1;
var year = dt.getFullYear();
document.write(day + '-' + month + '-' + year);

// Output: current month, day, year
</script>    
</body>
</html>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2017-05-23
@Masterocek

var dt = new Date();
var date = dt.getDate();
if( date < 15) dt.setDate(1); // до 15-го числа будет 1-е
else dt.setDate(15); // после 15-го – 15-е

// Display the month, day, and year. getMonth() returns a 0-based number.
var day = dt.getDate();
var month = dt.getMonth()+1;
var year = dt.getFullYear();
document.write(day + '-' + month + '-' + year);

E
Elvizz, 2017-05-23
@Elvizz

On the page for example:

<div> Current Time 
  <span id="clock"></span>
</div>

js file:
window.onload = function(){

function showClock(){
  var time = new Date();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var sec = time.getSeconds();
  var timeFormatter = digitFormatter(hour)+':'+digitFormatter(minute)+':'+digitFormatter(sec);
  var clock = document.getElementById("clock");

  
  clock.textContent = timeFormatter;
}

function digitFormatter(num){
  if(num<10){
    return '0'+num;
  }else{
    return num;
  }
}
showClock();
setInterval(showClock,1000)
}

S
Shane Matte, 2017-05-24
@mattedev

Cron

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question