What is the best way to create a calendar that displays every day in a single row?

Is it possible to create a calendar with all days in one row?

I've been searching for a solution to this problem without any luck. It's surprising that I haven't found a clear answer or explanation on how to achieve this.

I'm sure many people have accomplished this task before, but the information seems elusive. Does anyone here know how to do this using JavaScript or TypeScript?

Is there a way to display all calendar days in a single row?

Most calendar tables we come across in libraries like JQuery typically show 7 days in one row. However, I would like to see 30 days in a row instead of 7.

For example:

Click here for an example image.

Answer №1

A straightforward and leisurely approach:

var
   startDate = new Date( 2015, 0, 1 )
   , numOfDays = 30
   , days = []
   ;

function addDay( date ) {
   date.setDate( date.getDate() + 1 );
}

while ( numOfDays-- ) {
    var formattedDate = startDate.getDate() + '/' + (startDate.getMonth() + 1) + '/' + startDate.getFullYear();
    days.push(formattedDate);
    addDay(startDate);
}
console.log(days.join());

The output in the console will be:

1/1/2015,2/1/2015,3/1/2015,4/1/2015,5/1/2015,6/1/2015,7/1/2015,8/1/2015,9/1/2015,10/1/2015,11/1/2015,12/1/2015,13/1/2015,14/1/2015,15/1/2015,16/1/2015,17/1/2015,18/1/2015,19/1/2015,20/1/2015,21/1/2015,22/1/2015,23/1/2015,24/1/2015,25/1/2015,26/1/2015,27/1/2015,28/1/2015,29/1/2015,30/1/2015

I utilized a counter to loop through 30 iterations. You have the flexibility to adjust the condition for generating dates within a specific month. However, I find using Julian days more efficient compared to the above method.

Answer №2

Alright, so the key here is to create an array with a specific structure. Let's explore how we can generate this array.

First off, I'd like to introduce you to a library called Moment.js, which we'll be using as an example. Of course, you have the option of using vanilla JavaScript instead.

var moment = require('moment');
var resultArray = [];

var util = {};

/** 
 * input:
 *   month [Number]
 * output:
 *   totalDays [String]
 */
util.getDaysOfMonth = function(month){
  month = +month;
  var totalDays = '31';

  if( /[469]|(11)/.test(month) ){
    totalDays = '30';
  } else if (month === 2) {
    totalDays = moment().isLeapYear() ? '29' : '28';
  }

  return totalDays;
}

// Get current month of the year
var month = moment().get('month') + 1;

// current year
var year  = moment().get('year');
var days  = util.getDaysOfMonth(month);
for(var i = 1; i <= days; i++) {
  var str = month + '/' + i + '/' + year;
  resultArray.push(str);
}

Now that we have our array, you can proceed to format it as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Adding an active class to a link based on the current page and locale: A step-by-step

Looking to add an active class to the subheader menu on the active page, but facing issues when changing the locale in the link. While it works for links like my-site/my-page, it doesn't work for links like my-site/fr/my-page. Utilizing Storyblok head ...

How to properly escape JSON with hyphen or dash in AngularJS ngSrc

Having trouble displaying an image from a json url with dashes. I attempted to use bracket notation but it does not seem to be functioning: <img ng-src="{{image.['small-size'].url || image.thumb}}"> When using single quotes, my angular vi ...

It seems that BeautifulSoup and Selenium are unable to locate the div or text elements on the website

I have been attempting to utilize BeautifulSoup or Selenium in order to retrieve the Head to Head text or its corresponding div element on betexplorer (link provided below), but my efforts have unfortunately proved to be unsuccessful. Despite being able to ...

Is it possible to concurrently hot module reload both the server (.NET Core) and client (Angular)?

Using the command 'dotnet watch run' to monitor changes in server code and 'ng build --watch' for Angular code updates has been successful. It rebuilds the code correctly into directories "bin/" and "wwwroot/" respectively. myapp.cspro ...

Enhance your React application by using a personalized hook that allows you to trigger a function

After creating a custom hook to handle uploads to an AWS S3 bucket, I encountered a small issue. Rather than having the hook execute the logic directly, I decided to create an executable function to return instead. However, I am facing a problem where the ...

Is it possible to transform this nested promise into a chain instead?

This situation is just a scenario | then (items) | then (items, actions) getItems() | getActions(for:items) | apply(actions -> items) :promise | :promise | model <= items | | :sync ...

What is the best way to deactivate a hyperlink on a widget sourced from a different website?

Is there a way to remove the link from the widget I embedded on my website without access to other editing tools besides the HTML code provided? For instance, we have a Trustpilot widget at the bottom of our page that directs users to Trustpilot's web ...

Select the Month and Year from the Dropdown Menu

I'm attempting to manage the selection of "Month" and "Year" in a dropdown list. I have two fields, "Start Date" and "End Date". In the "Start Date" field, I am displaying the "Current Month" and "Year", for example, "March 2020". My goal is to restri ...

Why am I receiving a null response from my ajax request?

I have a working ajax call to fetch XML data from my REST API. However, when I try to echo the results, JQuery returns null. If I use var_dump on the results instead, JQuery accepts the information but it's not formatted correctly and causes errors. ...

Utilizing Window function for local variable assignment

Currently, I am facing a challenge in my Angular2 service where I am attempting to integrate the LinkedIN javascript SDK provided by script linkedin. The functionality is working as expected for retrieving data from LinkedIN, however, I am encountering an ...

What is the process for using dojo to load content?

As I work on creating a single-page website with the help of dojo, I refer to tutorials that explain how to utilize dojo/request for ajax requests. The process is straightforward - simply make a request, receive HTML content, and insert it into a designate ...

What can be done to prevent Leaflet from processing markers that fall outside of its bounding box?

I recently set up a Leaflet map with GeoDjango and rest_framework_gis, but I am facing issues with limited results and pagination. The map seems to process every marker it receives cumulatively, leading to lagging and browser crashes. I was advised in a co ...

How to update icon for fa-play using Javascript in HTML5

I recently added an autoplay audio feature to my website. I would like to implement the functionality to pause and play the music, while also toggling the icon to fa-play at the same time. This is the HTML code I am using: <script type="text/javascri ...

differences in opinion between JavaScript code and browser add-ons/extensions

As the owner and developer of an e-commerce website, I find that potential customers often contact us regarding ordering issues. Upon investigation, we consistently discover that the problem is caused by JavaScript errors. We frequently check their browse ...

What could be causing my application to hang on my local server?

Recently, I developed a NodeJS and Express MVC (or perhaps more accurately VC) app. Initially, everything worked smoothly until I integrated express-validator and included the middleware in the app file. This caused my localhost to freeze, displaying a GET ...

changing the css transformation into zoom and positioning

My goal is to incorporate pinch zoom and panning using hardware-accelerated CSS properties like transform: translate3d() scale3d(). After completing the gesture, I reset the transform and switch to CSS zoom along with absolute positioning. This method ensu ...

How can I showcase CSV data as clickable links and images on a website using HTML?

Looking for a way to display CSV file links as clickable hyperlinks in a table? Want to directly show images from photo links on your website as well? Wondering if this is even possible? Successfully showcased desired content in a table with the code prov ...

Ways to insert text into an SVG file

I am currently using vue-svg-map with a USA map. I am trying to display the state code (path.id) on my svg map. Can anyone provide guidance on how to achieve this? <radio-svg-map v-model="selectedLocation" :map="usa" :location-class="getLocation ...

Checking Whether a Value Entered in an Input Field Exists in an Array Using jQuery

Can someone help me with checking if a specific value is in my array? I want to achieve something like that, any suggestions on how to do it? if(jQuery.inArray($('#ValueInputTitle').val, variableValueInput) !== -1) { console.log("is in arr ...

Angular 5+ does not have any compatible call signatures for Type Search

Utilizing an addItem function that I found on this site, I encountered the following error message: Error TS2349 (TS) Cannot invoke an expression whose type lacks a call signature. Type 'Search' has no compatible call signatures. Definition o ...