Back

[SOLVED] Issues with Date.

  • 0
  • Web
RangerDev
23 May, 2023, 00:40

Hello. I am having issue with the dates in appwrite. For some reason, the date are printing out one day before the current day.

Here is code:

TypeScript
const today = new Date();
const formattedToday = today.toDateString().slice(4); // Format the date as "MM-DD-YYYY"

console.log(formattedToday);

const todayPosts = entries.documents
  .filter((post) => {
    const postDate = new Date(post.LaunchDate);
    console.log(postDate);
    console.log('____________________');
    const formattedPostDate = postDate.toDateString().slice(4); // Format the date as "MM-DD-YYYY"
    console.log('Formatted: ' +formattedPostDate);
    return formattedPostDate === formattedToday; // Filter posts with matching formatted date
  })
  .sort((a, b) => b.Upvotes.length - a.Upvotes.length);

const posts = entries.documents.sort(
    // sort the posts based on the number of upvotes
    (a, b) => b.Upvotes.length - a.Upvotes.length
);
TL;DR
The user is experiencing issues with dates in their appwrite application. The dates are printing out one day before the current day. Another user suggests that this could be due to the dates being in UTC. They provide a solution to adjust the timezone by adding or subtracting hours. The first user updates their code to match the timezone to UTC. Additionally, there is a discussion about Appwrite database and whether it is self-hosted or on the Appwrite cloud. Another user suggests a solution using Appwrite database to retrieve documents within the current day. Finally, the original user confirms that they are using Appwrite cloud and expresses their gratitude for the
Binyamin
23 May, 2023, 00:59

The date you're receiving are UTC

RangerDev
23 May, 2023, 01:00

oh

RangerDev
23 May, 2023, 01:02

I want to create somth like how product hunt has

RangerDev
23 May, 2023, 01:02

like how they show the current products of the day

Binyamin
23 May, 2023, 01:02

So you can add the hours (or subtract) to match your timezone

Binyamin
23 May, 2023, 01:02

Ohh cool

RangerDev
23 May, 2023, 01:02

but unsure how

RangerDev
23 May, 2023, 01:02
Binyamin
23 May, 2023, 01:02

When searching you should also match the timezone to UTC

RangerDev
23 May, 2023, 01:03

like this?

RangerDev
23 May, 2023, 01:03
TypeScript
const today = new Date();
console.log(today);
const formattedToday = today.toISOString().slice(0, 10); // Format the date as "YYYY-MM-DD"

console.log(formattedToday);

const todayPosts = entries.documents
  .filter((post) => {
    const postDate = new Date(post.LaunchDate);
    const formattedPostDate = postDate.toISOString().slice(0, 10); // Format the date as "YYYY-MM-DD"
    console.log(formattedPostDate);
    return formattedPostDate === formattedToday; // Filter posts with matching formatted date
  })
  .sort((a, b) => b.Upvotes.length - a.Upvotes.length);

const posts = entries.documents.sort(
  // sort the posts based on the number of upvotes
  (a, b) => b.Upvotes.length - a.Upvotes.length
);
Binyamin
23 May, 2023, 01:04

Are you using Appwrite database?

RangerDev
23 May, 2023, 01:04

Yes

RangerDev
23 May, 2023, 01:04

The post.launchdate

RangerDev
23 May, 2023, 01:05

It's from appwrite

Binyamin
23 May, 2023, 01:05

Is this against the cloud?

Binyamin
23 May, 2023, 01:06

Or latest Appwrite version?

RangerDev
23 May, 2023, 01:06

Wym

Binyamin
23 May, 2023, 01:06

Is it self-hosted or Appwrite cloud?

RangerDev
23 May, 2023, 01:06

I'm using appwrite cloud

Binyamin
23 May, 2023, 01:12

So for example if you have collection named votes, and you want to get all of those in the current day you can do this:

TypeScript
try{
  const start = new Date();
  start.setUTCHours(0,0,0,0);
  const end = new Date();
  end.setUTCHours(23,59,59,999);

  const docs = await documents.listDocuments('db_id','c_id',[
    Query.greaterThan('LaunchDate',start.toISOString()),
    Query.lessThan('LaunchDate',end.toISOString()),
  ]);
} catch (e) {
 // Mmm something went wrong,
}
Binyamin
23 May, 2023, 01:12

This will return all documents where LaunchDate match this dates.

Binyamin
23 May, 2023, 01:14

In the self-hosted version 1.3.x+ you can use a shorthand of Query.between You read about it here: https://dev.to/appwrite/join-celebrations-appwrite-13-ships-relationships-57fc

RangerDev
23 May, 2023, 01:16

Oh

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more