Skip to content

Commit

Permalink
New JS file to handle date options
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed Dec 6, 2024
1 parent 494cda2 commit 4b9e2b6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/assets/javascripts/assets/killbill_date_range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function setDateRange(option) {
var currentDate = new Date();
var startDate, endDate;

if (option === "day") {
startDate = new Date();
endDate = new Date();
endDate.setDate(endDate.getDate() + 1);
} else if (option === "week") {
startDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1));
currentDate = new Date();
endDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 7));
} else if (option === "month") {
startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
endDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
} else if (option === "year") {
startDate = new Date(currentDate.getFullYear(), 0, 1);
endDate = new Date(currentDate.getFullYear(), 11, 31);
}

var startDateFormatted = formatDate(startDate);
var endDateFormatted = formatDate(endDate);

$('#startDate').val(startDateFormatted);
$('#endDate').val(endDateFormatted);
$('#startDate, #endDate').prop('disabled', true);
}

function formatDate(date) {
var year = date.getFullYear();
var month = String(date.getMonth() + 1).padStart(2, '0');
var day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}

0 comments on commit 4b9e2b6

Please sign in to comment.