From 4b9e2b6dcf39e8f688fb93f5d89e0dd82fb1a845 Mon Sep 17 00:00:00 2001 From: Kyle Le Date: Fri, 6 Dec 2024 22:12:34 +0700 Subject: [PATCH] New JS file to handle date options --- .../javascripts/assets/killbill_date_range.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 app/assets/javascripts/assets/killbill_date_range.js diff --git a/app/assets/javascripts/assets/killbill_date_range.js b/app/assets/javascripts/assets/killbill_date_range.js new file mode 100644 index 0000000..8d35acf --- /dev/null +++ b/app/assets/javascripts/assets/killbill_date_range.js @@ -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}`; +}