Efficiently organizing dates in the Vuetify date range picker

I am working with a vuetify date range picker component and have the following code:

                    <v-menu ref="effectiveDateMenu"
                        v-model="effectiveDateMenu"
                        :close-on-content-click="false"
                        transition="scale-transition"
                        offset-y
                        max-width="290px"
                        min-width="auto">
                    <template v-slot:activator="{ on, attrs }">
                        <v-text-field label="Time Period"
                                      v-model="effectiveDateRange"
                                      filled
                                      dense
                                      rounded
                                      @*:rules="[v => !!(v && v.length) || 'Rating period is required']"*@
                                      v-bind="attrs"
                                      v-on="on"
                                      range
                                      required
                                      :class="toggleEdit ? 'locked' : 'editable'"
                                      :readonly="toggleEdit"></v-text-field>
                    </template>
                    <v-date-picker v-model="dates"
                                   no-title
                                   @@input="datesPicked()"
                                   range></v-date-picker>
                </v-menu>

I am facing an issue where I cannot get the selected dates to display in the correct order. Even when using the sort function on the v-model for the date picker, if I select the newer date first, it displays that value first in the text field. I have tried implementing a computed property as shown below, but still unable to resolve the ordering problem:

            computed: {
            effectiveDateRange: function () {
                // `this` refers to the vm instance

                return this.dates.map((element) => {
                    var d = new Date(element);
                    return `${d.getUTCMonth() + 1}/${d.getUTCDate()}/${d.getUTCFullYear()}`;
                }).sort(function (a, b) {
                    // Convert strings into dates and compare them
                    return new Date(b.date) - new Date(a.date);
                }).join(' - ')
            }
        }

Answer №1

Given that the elements within the dates array are presented in strings following the ISO 8601 format (YYYY-mm-dd or YYYY-mm), with dates and times arranged from greatest to smallest importance, a straightforward method of sorting the array is:

effectiveDateRange: function() {
  return this.dates.sort().join(' - ');
}

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

Retrieve the attributes associated with a feature layer to display in a Pop-up Template using ArcGIS Javascript

Is there a way to retrieve all attributes (fields) from a feature layer for a PopupTemplate without explicitly listing them in the fieldInfos object when coding in Angular? .ts const template = { title: "{NAME} in {COUNTY}", cont ...

What could be causing my MongoDB Node.js driver's $lookup aggregation query to not produce the expected results?

In my database, I have two collections: users and roles Each user has an array of roles assigned to them: { _id : 61582a79fd033339c73ee290 roles:[615825966bc7d715021ce8fc, 615825966bc7d715021ce8fd] } The roles are defined as follows: [ { ...

Guide to organizing files with custom directory layout in NodeJS

Imagine having multiple files spread across various folders. /parentDir/ |__dirA/ |__file1 |__file2 |... |__dirB/ |__file3 |... |... You want to consolidate them into an archive, with the option of using something like ...

Using `babel/register` does not seem to be functioning as expected

I am working on an isomorphic app built in ES6 on the client side using the Babel transpiler. I would like my express server to also utilize the same ES6 syntax as the client code. Unfortunately, attempting to use require('babel/register') has n ...

Struggling to forward JSON data via AJAX to Django without relying on jQuery

My goal is to utilize AJAX in conjunction with Django, but I want to achieve this without relying on jQuery. So far, I have made progress in sending URL encoded data from AJAX to Django successfully. However, I am currently facing an issue where I am unabl ...

Is there a way to sort through a JSON object using JavaScript?

I have a JSON string that looks like this: { "Animal":{ "Cat":20, "Dog":10, "Fish":5 }, "Food":{ "Pizza":500, "Burger":200, "Salad" ...

Exploring ways to retrieve boolean values with Angular and PHP

I am currently learning Angular and PHP and I am trying to make some modifications to the tutorial found at . Specifically, I want to change the second value to a checkbox and retrieve its value using both Angular and PHP. However, I am encountering an iss ...

Sorting dates in Vue JS can be achieved effortlessly by utilizing components and Computed Properties

I attempted to arrange the dates using computed properties but encountered issues. However, when I utilized methods and removed the slice method, the sorting worked as expected. I also tried splitting the date without success. The root cause of the problem ...

adding numerous items to an array using JavaScript

How can I add multiple objects to an array all at once? router.post(`/products`, upload.array("photos" , 10), async (req, res) => { console.log(res); try { let product = new Product(); req.files.forEach(file => { product.p ...

Replicating JavaScript functions with the power of Ajax

I'm facing an issue with Bootstrap modal windows on my page. The modals are opening and closing successfully, but the content inside them is fetched through AJAX as HTML. For example, there's a button in the modal: <button id="myBtn"> and ...

I'm experiencing an issue where posts are duplicated based on the number of comments they have. Any suggestions on how to

Currently, I am working on a project involving lumen and Vuejs. In this project, I have performed a left join operation between my 'likes' and 'comments' tables with my posts table. While the likes data is being retrieved correctly with ...

Error: The function setIsEnabled does not exist

Currently, I am in the process of merging two separate next.js projects to create a website that can utilize the Cardano wallet 'Nami'. The code for accessing the wallet functions correctly in its original project, but when transferred over, it p ...

What is the reason that .bin/www is recognized as a JavaScript file even though it does not have the .js extension when utilizing express-generator

Can you explain why .bin/www is recognized as a JavaScript file by express-generator even without the .js extension? Whenever I create a bin/ folder with a www file inside, it's automatically identified as a JavaScript file despite the missing .js ex ...

Trouble Connecting Local Database with PG NPM Package

I'm encountering issues with using the pg package on my computer. I've attempted to execute the following code: var pg = require('pg'); var con_string = "postgres://user:password@localhost:5432/documentation"; var client = new pg.Clie ...

Not all API results are being displayed by the Nextjs API function

I am facing an issue with rendering all the returns from the API in my Next.js application. The API, which is created in Strapi, is only displaying 1 out of the 3 possible returns. I am a beginner when it comes to using APIs and I suspect that the issue li ...

Switch back and forth between two tabs positioned vertically on a webpage without affecting any other elements of the page

I've been tasked with creating two toggle tabs/buttons in a single column on a website where visitors can switch between them without affecting the page's other elements. The goal is to emulate the style of the Personal and Business tabs found on ...

What is the process for enabling text compression in a Laravel and Vue project?

Upon analyzing the lighthouse report, I attempted to implement the suggested solution in my .htaccess file. However, the network content-encoding was not displayed as expected. I tried utilizing GZIP compression. <IfModule mod_rewrite.c> <IfMo ...

Exploring the vertices of a single face of a cube using three.js

My current project involves manipulating the x position of all coordinates on a single face of a cube. Here is my current method: var wDepth = 200; var hDepth = 200; var geo = new THREE.CubeGeometry( 20, 40, 40, 20, wDepth, hDepth); for ( var i = ...

Using PHP and JavaScript to determine device screen width and eliminate the $_GET parameters from the URL

I have implemented the following JavaScript code to detect screen width and utilize it as a constant throughout my template files using conditional statements to control the visibility of different sections on my site. Just to clarify, I am working within ...

Tips on comparing a string against an object's value

I need to compare the key values of an object with the strings yes or no. I am encountering difficulties in achieving this comparison and updating radio buttons accordingly based on the comparison. Here is the code snippet: const screenJson = { Managem ...