skip to Main Content

I have to select the data according to the date and I had store the according to the week like a book slot app I am creating a appointment booking app

I am trying to get data from the firebase but I set the acc to the week

2

Answers


  1. In fact, I am not sure your problem. I copied some code from my repo, hope you find helpful.

    return XXX days ago or XXX hours ago or XXX minutes ago, etc.

    import dayjs from "dayjs";
    
    dayjs.extend(relativeTime);
    
    //return XXX days ago or XXX hours ago or XXX minutes ago, etc.
    export function fromNow(date: string | Date): string {
      return dayjs(date).fromNow();
    }
    

    Generate week and day

    let currentDate = new Date();
    
    let nextWeek = new Date();
    nextWeek.setDate(currentDate.getDate() + 7);
    
    for (let i = 0; i < 7; i++) {
      let date = new Date();
      date.setDate(currentDate.getDate() + i);
      let weekDay = date.toLocaleString('en-US', { weekday: 'short' });
      console.log(weekDay + ': ' + date.toLocaleDateString());
    }
    Login or Signup to reply.
  2. To retrieve data from Firebase according to the week, you’ll need to store the data in a structure that allows you to query it efficiently based on the week. One way to do this is by using Firebase Realtime Database or Firestore and structuring your data as follows:

    1. Use the ISO week date format: To represent the week, use the ISO week date format, which represents dates by year and week number. This format ensures that weeks always start on a Monday and each week has seven days.

    2. Create a node for appointments: Under your Firebase database, create a node to store all the appointments. Each appointment should have a unique identifier, such as a generated key by Firebase.

    3. Store the appointments under the week nodes: Create a separate node for each week and store the appointments for that week under this node. The week node could be named using the ISO week date format, like "YYYY-WW" (e.g., "2023-31" for the 31st week of 2023).

    Here’s an example of how the data structure might look in Firebase Realtime Database:

    {
      "appointments": {
        "2023-31": {
          "appointment1": {
            "title": "Meeting with Client",
            "datetime": "2023-08-01T15:30:00"
          },
          "appointment2": {
            "title": "Team Standup",
            "datetime": "2023-08-03T10:00:00"
          },
          // Other appointments for week 31 of 2023
        },
        "2023-32": {
          // Appointments for week 32 of 2023
        },
        // Other weeks
      }
    }
    

    To retrieve appointments for a specific week, you can use Firebase queries. For example, if you want to get appointments for week 31 of 2023, you can do the following (assuming you are using the Firebase Realtime Database and using JavaScript):

    const database = firebase.database();
    const weekRef = database.ref('appointments/2023-31');
    
    weekRef.on('value', (snapshot) => {
      const appointments = snapshot.val();
      // Now you have the appointments for week 31, do what you need with the data.
    });
    

    Keep in mind that the above example is based on Firebase Realtime Database. If you are using Firestore, the structure and query will be slightly different, but the concept remains the same.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search