Getting a date range adjust to days - javascript

I have this date functionality that gets date range of six months, taking into consideration if the range has passed to a new year. How can i alter this date function to take a range of 15 days,while taking into consideration if the range has passed to a new months and/or a new year.
var dt = new Date();
var month = new Array();
month[0] = "JAN";
month[1] = "FEB";
month[2] = "MAR";
month[3] = "APR";
month[4] = "MAY";
month[5] = "JUNE";
month[6] = "JULY";
month[7] = "AUG";
month[8] = "SEPT";
month[9] = "OCT";
month[10] = "NOV";
month[11] = "DEC";
var tmonth = dt.getMonth() + 6 <= 11 ? dt.getMonth() + 6 : dt.getMonth() - 6;
var tyear = dt.getMonth() + 6 <= 11 ? dt.getFullYear() : dt.getFullYear() + 1;
var eom = new Date(tyear, tmonth + 1, 0).getDate(); //get last day of the month
var tday = dt.getDate() > eom ? eom : dt.getDate(); //check if the from day of month > last day of month
var frm = dt.getDate() + '-' + month[dt.getMonth()] + '-' + dt.getFullYear().toString().substr(2, 2);
var till = tday + '-' + month[tmonth] + '-' + tyear.toString().substr(2, 2);

I think the solution to this would be:
var dt = new Date();
var month = new Array();
month[0] = "JAN";
month[1] = "FEB";
month[2] = "MAR";
month[3] = "APR";
month[4] = "MAY";
month[5] = "JUNE";
month[6] = "JULY";
month[7] = "AUG";
month[8] = "SEPT";
month[9] = "OCT";
month[10] = "NOV";
month[11] = "DEC";
var range = 15;
var tmonth = dt.getMonth() + (range % 12) <= 11 ? dt.getMonth() + (range % 12) : dt.getMonth() - 6;
var tyear = dt.getMonth() + (range % 12) <= 11 ? dt.getFullYear() + Math.floor(range/12) : dt.getFullYear() + Math.floor(range/12) + 1;
var eom = new Date(tyear, tmonth + 1, 0).getDate(); //get last day of the month
var tday = dt.getDate() > eom ? eom : dt.getDate(); //check if the from day of month > last day of month
var frm = dt.getDate() + '-' + month[dt.getMonth()] + '-' + dt.getFullYear().toString().substr(2, 2);
var till = tday + '-' + month[tmonth] + '-' + tyear.toString().substr(2, 2);
Now you can choose any range you want - whether it's 6 or 15.

Related

display current month until the 15th day, then to next month after the 15th day. E.g: If July 10 - display “July”, If July 20 - display “August”,

I've written script for both current month & following month (shown below). However, I am trying to achieve something in the middle; displaying the current month until the 15th day, then changing to next month after the 15th day.
Example:
If today is July 10th, display “July”.
If today is July 20th, display “August”.
Displaying the Current Month, alternating: “var n = month[d.getMonth()];”
{
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()];
document.write (month = n)
}
Displaying the Following Month, alternating: “var n = month[d.getMonth()+1];”
{
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()+1];
document.write (month = n)
}
I've experimented with values in between the 0-1, but still no luck. I haven't managed to find a solution after a lot of research so any help would be much appreciated.
Thanks in advance,
Ryan.
You can get the day of the month with new Date().getDate(). Then, if it is more than 15, add 1 to the month index to return.
Also, I rewrote your months array in a simple form instead of month[0] = "January";
{
var month = ["January","February","March","April","May","June","July","August","September","October","November","December"]
var d = new Date();
var dayOfMonth = d.getDate();
var n = month[ d.getMonth() + (dayOfMonth > 15 ? 1 : 0) ];
document.write ("month = " + n)
}
We can create a function to return the month name from the month, and a function to get the next month.
Then, if the current day is 15 or greater, we'll show the next month:
function getMonthName(month, locales = 'default') {
return new Date(new Date().getFullYear(), month, 1).toLocaleString(locales, { month: 'long'} );
}
function getNextMonth(month) {
return (month + 1) % 12;
}
function getMonthToDisplay(date) {
let month = date.getMonth();
if (date.getDate() >= 15) {
month = getNextMonth(month);
}
return getMonthName(month);
}
let dates = ["2021-06-14T18:00:00Z","2021-06-15T00:00:00Z", "2021-09-30T00:00:00Z", "1965-11-01T15:00:00Z"].map(dt => new Date(dt));
for(let d of dates) {
console.log("Date:", d.toDateString(), "Current:", getMonthName(d.getMonth()), "Display:", getMonthToDisplay(d))
}
console.log("Testing all days in June 2021...");
dates = [...Array(30).keys()].map((v,k) => new Date(2021, 5, k + 1));
for(let d of dates) {
console.log("Date:", d.toDateString(), "Current:", getMonthName(d.getMonth()), "Display:", getMonthToDisplay(d))
}
You can get
date -> new Date().getDate()
month -> new Date().getMonth()
and compare date with 15 and get the desired result.
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
const now = new Date();
const mon = now.getMonth();
const date = now.getDate();
const result = date > 15 ? month[mon + 1] : month[mon];
console.log(result);
document.write(result)

Javascript calendar displaying month and days

I'm trying to display a calendar where the month and year show at the top, with the days on single line (number line format) below. I want to get the next and previous months after clicking a button.
However, it only works after one click and I cant get the correct amount of days to display for the month.
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var label = month[d.getMonth()];
var year = d.getFullYear();
var day = d.getDate();
document.getElementById("mon").innerHTML = label + " " + year;
document.getElementById("days").innerHTML = day;
//display days
var daysInMonths = [31, (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
document.getElementById("prevBtn").addEventListener("click", previousMonth);
function previousMonth() {
label = month[d.getMonth() - 1];
document.getElementById("mon").innerHTML = label + " " + year;
}
document.getElementById("nextBtn").addEventListener("click", nextMonth);
function nextMonth() {
label = month[d.getMonth() + 1];
if (label > 11) {
year = d.getFullYear() + 1;
}
document.getElementById("mon").innerHTML = label + " " + year;
}
<div class="calendar">
<div class="header">
<span class="left button" id="prevBtn">〈</span>
<span><h1 id="mon"></h1></span>
<span class="right button" id="nextBtn">〉</span>
</div>
<table>
<tbody>
<tr id="days"></tr>
</tbody>
</table>
</div>
I've made a few modifications below as per my comment above.
var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var today = new Date();
var label = month[today.getMonth()];
var mm = today.getMonth(); // keep track of the month
var year = today.getFullYear(); // keep track of the year
var day = today.getDate();
document.getElementById("mon").innerHTML = label + " " + year;
document.getElementById("days").innerHTML = day;
//display days
var daysInMonths = [31, (((year%4==0)&&(year%100!=0))||(year%400==0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
document.getElementById("prevBtn").addEventListener("click", previousMonth);
function previousMonth() {
mm -= 1; // decrement the month
if (mm < 0) { // if we go before January
mm = 11; // go to December
year -= 1; // of previous year
}
label = month[mm];
document.getElementById("mon").innerHTML = label + " " + year;
}
document.getElementById("nextBtn").addEventListener("click", nextMonth);
function nextMonth(){
mm += 1; // increment month
if (mm > 11) { // if we go after December
mm = 0; // go to January
year += 1; // of next year
}
document.getElementById("mon").innerHTML = label + " " + year;
}

Javascript Date +15 Days

I want this day to be 25 days from now, and it needs to correspond with normal workday measurements. Saturdays, Sundays and Holidays need to be removed. I've tried using Google sheets and javascript:
Here is what I have.
function Calendar() {
var now = new Date();
var year = now.getFullYear();
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";
var monthnum = month[now.getMonth()];
var weekday = new Array();
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var day = weekday[now.getDay()];
var date = now.getDate();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Schedule");
var last = sheet.getLastRow();
var destination = sheet.getRange(last, 1);
var format = sheet.getRange(10, 1, 1, 7);
var insertdate = sheet.getRange(last, 2);
var insertday = sheet.getRange(last, 1)
sheet.insertRowAfter(last);
sheet.deleteRow(7)
insertdate.setValue(monthnum + "/" + date + "/" + year);
insertday.setValue(day)
Logger.log(monthnum + "/" + date + "/" + year + " " + day)
}
There are many ways to do it, but the easily one is: 86400000 is one day
86400000= 24 Hours * 60 Min * 60 Seconds * 1000 Milliseconds
Here is an example which may help you:
var first = new Date();
var last = new Date(new Date(first).getTime() + (86400000 * 25));
var now = new Date(); now.setDate(now.getDate() + 25);
console.log(first);
console.log(last);
console.log(now);

Get month number from a date in javascript

I have a daterangepicker function that is returning the selected date in this format 06 May 2016. What I am trying to do is extract the month as an integer, therefore from the above I should be able to return the number 5.
This is the line of code that returns the selected date - getDateString(new Date(opt.start)
Any help is appreciated thanks.
var date = new Date();
var month = date.getMonth();
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
var datestring = getDateString(new Date(opt.start);
var monthNumber = new Date(datestring).getMonth()+1;
I'm not sure this will help since I am new in this field
var today = new Date();
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";
var monthNumber = month[today.getMonth()];
console.log("This month is " + monthNumber);
Thanks, and more simple:
var month = new Date().getMonth() + 1;
// Result, ie: January = 1 ... December = 12
var month = new Date().getMonth() + 1;
console.log("month", month);

My highlighted current date via javascript is not showing correctly

I have successfully managed (with the help from stack overflow) to loop through a calendar starting from may until end oktober 2015. now i want to highlight the current date which is 28 june sunday. but for some reason it highlights the wrong date and i don't think its because of the code but i am not a 100% sure.
checkout this link to see it visually (http://gyazo.com/30e448d0f84c8f1c55e4dd1ce6d91f38)
jsfiddle link: https://jsfiddle.net/GY22/vqfk8yLL/
this is my html code:
<ul id="timeline">
<li></li>
</ul>
this is my javascript code:
<script>
// Get today's current date.
var now = new Date();
console.log(now);
// Calculates the number of the week
var weekNumber = ((now.getDate()<10) ? "0" : "")+ now.getDate();
console.log("The current week number is: " + weekNumber);
// Array list of months.
var month = new Array(12);
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
//console.log(month[3]);
var weekDay = new Array(7);
weekDay[0]= "Su";
weekDay[1] = "Mo";
weekDay[2] = "Tu";
weekDay[3] = "We";
weekDay[4] = "Th";
weekDay[5] = "Fr";
weekDay[6] = "Sa";
function formatDate(date) {
var month = date.getUTCMonth() +1;
var dayNumber = date.getUTCDate();
var year = date.getUTCFullYear();
var day = date.getUTCDay();
return weekDay[day] + ": " + dayNumber + "-" + month + "-" + year + "; ";
//return weekDay[day] + " " + dayNumber + "; ";
}
//console.log(formatDate(new Date()));
var today
function addListItem(){
var createListItem = document.createElement("li");
var outputListItem = document.createTextNode(today);
createListItem.appendChild(outputListItem);
var createUl = document.getElementsByTagName("ul");
createUl[0].appendChild(createListItem);
}
// loop starting from may up untill for months from the set date
for (var i = 0; i < 122; i++){
today = formatDate(new Date(2015, 05, i));
//document.write(today + "<br />");
addListItem();
}
document.getElementById('timeline').
getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';
Change the last line from:
getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';
To:
getElementsByTagName('li')[(new Date()).getDate() + 1].className += ' currentDate';
The date appeared to be off by just one day before, so that should fix it.
Consider using MomentJS instead. It would save you a lot of code and date problems:
http://momentjs.com/
If you prefer to keep your code, move your current date logic inside your addListItem() method.
if(today == formatDate(new Date())) {
createListItem.className += ' currentDate';
}

Categories

Resources