Margata
Margata

Reputation: 1

Javascript - Change the first day of month

I am developing a simple custom module, that says "The current month is: Current month", here is the script:

<span class="b" id="monthis"> </span>
<script type="text/javascript">

var month = new Array(12);
month[0]="Януари";
month[1]="Февруари";
month[2]="Март";
month[3]="Април";
month[4]="Май";
month[5]="Юни";
month[6]="Юли";
month[7]="Август";
month[8]="Септември";
month[9]="Октомври";
month[10]="Ноември";
month[11]="Декември";

var current_date = new Date();
month_value = current_date.getMonth();
day_value = current_date.getDate();
year_value = current_date.getFullYear();
document.getElementById("monthis").innerHTML = (month[month_value] + "  " + year_value);

</script>

Everything works, the month changes in 1-st day of each month, but I need to make it change on the 3-rd day of current month. For example: To switch to the next month (December), the module has to wait 2 days, and switch the month on the 3-rd day, e.g. the month December has to start from 03.12.2015 up to 02.01.2016, and this shift has to be for every month.

Upvotes: 0

Views: 470

Answers (2)

Dmytro Pastovenskyi
Dmytro Pastovenskyi

Reputation: 5419

Just make -2 days to the current date and keep same logic.

var current_date = new Date();
current_date.setDate(current_date.getDate()-2);

Upvotes: 2

Syel
Syel

Reputation: 31

A possible solution would be to add a condition checking day_value, and then changing the month (and year) if needed:

if(day_value < 3){
    if(month_value - 1 < 0){
        month_value = 11;
        year_value--;
    } else {
        month_value--;
    }
}

Edit: My first answer didn't account for year changes. It should be handled with this condition.

JSFiddle Example

Upvotes: 0

Related Questions