Problems to convert/cut TIME from 14:00:000.00 to 14:00 in a dropdown-list

hey,

I really hope someone can help with my linked repeater:
2 ISSUES I´m working on since hours:
FIRST: how to convert the time from 14:45:00.00 to 14:45 as options in the dropdown.
I do get the right value´S in the console.log but not into the dropdown :slight_smile:
SECOND: When using the unshift-function to add “all” as additional option, I get an empty repeater as result. i tried to set the value to ‘’ , “” . nothing works.

see code below the picture and many thx for any helpfull advices.


my code:

import wixData from ‘wix-data’;

$w.onReady( async function () {

wixData.query(“test”)
.limit(1000)
.find()
//.ascending(“signInDay1”)
.then(results => {
const uniqueItems = getUniqueItems(results.items);
$w(“#dropdownTime”).options = buildOptions(uniqueItems);
let cleanList = buildOptions(uniqueItems);
cleanList.unshift({ label: ‘All’, value: “” });

// to convert/cut the time to 4 digits (5 plus :slight_smile:
for ( var i = 0; i < uniqueItems.length; i++) {
var timestring = uniqueItems[i][0] + “” + uniqueItems[i][1] + “” + uniqueItems[i][2] + “” + uniqueItems[i][3] + “” + uniqueItems[i][4];
uniqueItems[i] = timestring;

            console.log(timestring); //console brings the right value/RESULT, but doesnt show up the way I want in the dropdown = 14:00:000.00 instead of 14:00 
            console.log(cleanList) 
          } 
        $w("#dropdownTime").options = cleanList;    //buildOptions(uniqueItems); 
}); 

function getUniqueItems(items) {
const itemsOnly = items.map(item => item.signInDay1);
return [… new Set(itemsOnly)];
}

function buildOptions(uniqueList) {
return uniqueList.map(curr => {
return { label: curr , value: curr };
});
}
});

export function dropdownTime_change(event) {
let signInDay1Value = $w(“#dropdownTime”).value;
$w(“#dataset2”).setFilter(wixData.filter().eq(“signInDay1”, signInDay1Value));
}

In “FIRST: how to convert the time from 14:45:00.00 to 14:30 as options in the dropdown.”, did you really mean "14:45:00.00 to 14: 45 " in lieu of "14:45:00.00 to 14: 30 "?

Sorry, mistyped.
Of course I mentioned 14:45:00.000 to 14:45.
In short:
I want to “cut” the time to a nice readable time.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

https://stackoverflow.com/questions/17913681/how-do-i-use-tolocaletimestring-without-displaying-seconds

hi @givemeawhisky ,

thx for response.
The thing is, I‘m totally new to this.
After reading and watching ton of Infos and wix API´s I‘m pretty happy with things I have already achieved.

But the links you sent me, are a bit to much for me.
I keep reading and trying. :slight_smile:

anyway, thx :+1:

Hi, Thomas.

This might be an easier way to effect the desired result:

// Retrieve just the first five/5 characters of the time
<time>.substring(0, 5);
1 Like

Hi @abergquist ,

thx; any idea how to implement it to the Dropdow—list (Code)?
As written before: i do get the right values in the consol.log aswell, but miss something, to get those values to the Dropdown-Menue.

To confirm, when you say, “console brings the right value/RESULT, but doesnt show up the way I want in the dropdown = 14:00:000.00 instead of 14:00”, I wanted to confirm that you are seeing “14:00” in the console, not “14:00:000.00”, correct?

yes sir,
14:00 is shown in the console but not in the repeater. (see picture)
it seems I MESS it up with my function to remove duplivates (uniqueItems) and can´t figure out how to combine them. :wink:

Does replacing the following line in the buildOptions() function:

  return { label: curr , value: curr }; 

with the following:

  return { label: curr.substring(0, 5) , value: curr }; 

effect the result you desire?

1 Like

@abergquist
jesus christ, you are my hero.
this is absolutly the result I want!
I can´t even so how much I THANK YOU!!!

[@Thomas Reinermann] @abergquist has given you the best solution but you will need to remove some code. I have provided your code and fixed it with an explanation below.

Here is your original code (from above) with my feedback showing you the problems.

import wixData from 'wix-data';

$w.onReady(async function () {
    wixData.query("test")
    .limit(1000)
    .find()  
    //.ascending("signInDay1")
    .then(results => {
        const uniqueItems = getUniqueItems(results.items);
//---------------------------------------------------------------------//
//  The best way to achieve the modifications required is to change the 
//  code in buildOptions before making the assignment to options.
//---------------------------------------------------------------------//       
        $w('#dropdownTime').options = buildOptions(uniqueItems);
//---------------------------------------------------------------------// 
//  The cleanList and unshift command needs to happen before the options
//  are assigned  
//---------------------------------------------------------------------// 
        let cleanList = buildOptions(uniqueItems);
        cleanList.unshift({ label: 'All', value: "" });
        
//---------------------------------------------------------------------//
//  At this point cleanList has been set and does not change again. 
//  The processing that follows manipulates the array uniqueItems but NEVER
//  changes the cleanList which is assigned for a second time below :-)
//
//  Additionally the for loop below essentially performs the same work as the 
//  map() function. It takes each item in the uniqueItems array (uniqueItem[i]) 
//  and combines characters from each string into a new string.
//---------------------------------------------------------------------//
           
  
        // to convert/cut the time to 4 digits (5 plus :)
        for (var i = 0; i < uniqueItems.length; i++) {
        
//---------------------------------------------------------------------//
//  The following assignment to timestring...
// var timestring = uniqueItems[i][0] + "" + uniqueItems[i][1] + "" + uniqueItems[i][2] + 
//                  "" + uniqueItems[i][3] + "" + uniqueItems[i][4];
// results in the same value as the substring command proposed by abergquist...
// var timestring = uniqueItems[i].substring(0,5);
//---------------------------------------------------------------------//
            var timestring = uniqueItems[i][0] + "" + uniqueItems[i][1] + "" + uniqueItems[i][2] + "" + uniqueItems[i][3] + "" + uniqueItems[i][4];
            uniqueItems[i] = timestring;
            console.log(timestring);
            //console brings the right value/RESULT, but doesnt show up the way I want in the dropdown = 14:00:000.00 instead of 14:00
            console.log(cleanList)
        }
        
//---------------------------------------------------------------------//
//  cleanList used below is the exact same data as highlighted above.
//---------------------------------------------------------------------//
        $w('#dropdownTime').options = cleanList;
        //buildOptions(uniqueItems);
    });

    function getUniqueItems(items) {
        const itemsOnly = items.map(item => item.signInDay1);
        return [...new Set(itemsOnly)];
    }
    
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return { label: curr , value: curr };
        });
    } 
});

export function dropdownTime_change(event) {
    let signInDay1Value = $w('#dropdownTime').value; 
    $w("#dataset2").setFilter(wixData.filter().eq("signInDay1", signInDay1Value));
}

Here is my proposal, using abergquist’s suggestion for your revised code.

import wixData from 'wix-data';

$w.onReady(async function () {
    wixData.query("test")
    .limit(1000)
    .find()  
    //.ascending("signInDay1")
    .then(results => {
 
        const uniqueItems = getUniqueItems(results.items);
//---------------------------------------------------------------------//
//  Call buildOptions to set up the options list AND adjust the format of the 
//  dropdown label to display in the format "xx:xx"
//---------------------------------------------------------------------//
        let cleanList = buildOptions(uniqueItems);
//---------------------------------------------------------------------//
//  Insert a special 'All' dropdown selection that resets the filter
//---------------------------------------------------------------------//
        cleanList.unshift({ label: 'All', value: "" });
//---------------------------------------------------------------------//
// Assign the dropdown options list
//---------------------------------------------------------------------//
        $w('#dropdownTime').options = cleanList;
    });
});



function getUniqueItems(items) {
    const itemsOnly = items.map(item => item.signInDay1);
    return [...new Set(itemsOnly)];
}
//---------------------------------------------------------------------//
//  This function create's the options to show in the drop down list
//  This is where the reformatting of the time string needs to occur
//---------------------------------------------------------------------//
function buildOptions(uniqueList) {
//---------------------------------------------------------------------//
// As mentioned above the map function performs a for loop that processes 
// each element in the uniqueList array passed as an argument
//---------------------------------------------------------------------//
    return uniqueList.map(curr => {
//---------------------------------------------------------------------//
//  curr is equivalent to uniqueList[i]
//  we need to adjust the value to be in the format "xx:xx"
//---------------------------------------------------------------------//
        let adjustedTime = curr.substring(0,5);
        return { label: adjustedTime , value: curr };
    });
} 


export function dropdownTime_change(event) {
    let signInDay1Value = $w('#dropdownTime').value; 
    $w("#dataset2").setFilter(wixData.filter().eq("signInDay1", signInDay1Value));
}

Good luck.

2 Likes

@stcroppe Well done!

@stcroppe
wooooow;
I´m impressed.
thanks for your effort and letting me understand a little more.
step by step I´m learning.
small steps for sure, but I´m happy as a newbi :wink:

but …
SOMEHOW with your code, I´m back to a little bug:
when selecting “all”, I get an empty repeater (totally nothing) as result.
any idea, sir?