Back

Get server timestamp

  • 0
  • Databases
  • Web
  • Cloud
-N I N J A-
17 Dec, 2023, 16:25

But it is not fixed to only hours also sometimes I need in minutes, so there will be so many functions execution each mins Ok thanks

TL;DR
To hide data, you can make the collection private and expose the data through a function. To get the server timestamp, you can use the following steps: 1. Get the current timestamp. 2. Subtract 7 days from it. 3. Convert it to ISO 8601 in UTC. Here's an example query for fetching a list of posts from the past 1 week: ```javascript appwrite.listDocs(..., [ Query.greaterThan( 'created', new Date(appwrite.serverTimestamp().setDate(appwrite.serverTimestamp().getDate() - 7) ) ] ); ``` Creating a
-N I N J A-
17 Dec, 2023, 16:32

so I need to do it like this??

-N I N J A-
17 Dec, 2023, 16:32

try { await appwriteDatabases .createDocument( DATABASE_ID, BANNED_USERS_COLLECTION_ID, getUser.$id, { user: getUser.$id, created: "", reasons: "Violence", } ) .then(async (data) => { const originalDateTime = new Date(data.created);

TypeScript
        const updatedDateTime = new Date(
          originalDateTime.getTime() + 5 * 60 * 60 * 1000
        );

        await appwriteDatabases.updateDocument(
          DATABASE_ID,
          BANNED_USERS_COLLECTION_ID,
          getUser.$id,
          {
            expiry: updatedDateTime,
          }
        );
      });
    log("banning_done2");
  } catch (updateDocumentError) {
    log("Update document error:", updateDocumentError);
  }
D5
17 Dec, 2023, 16:32

You can trigger a function each minute

-N I N J A-
17 Dec, 2023, 16:50

There is problem when creating new doc or update existing doc with same Id, Is there upsertDocument available in appwrite. I manually need to check if doc exist ?update :create doc

Drake
17 Dec, 2023, 19:20

There shouldn't be any need to get the servers time or time zone.

Best practice when working with remote servers is to always handle remote timestamps in UTC.

So, you can store the unban timestamp or a banned timestamp and expiry hours and then localize the time client side

-N I N J A-
18 Dec, 2023, 19:45

But there should be a function like appwrite.getTimestamp(), If I want to fetch some docs from now to in the past in client side using query but using functions is complicated.

D5
18 Dec, 2023, 19:46

If you get server timestamp client side, it will be possible to manipulate it anyways

D5
18 Dec, 2023, 19:46

To override the timing cooldown

D5
18 Dec, 2023, 19:47

That's why I recommend handling all with an Appwrite function so since it's being created server sided, it will not be possible to manipulate it to unban the user sooner than at the expected time

-N I N J A-
18 Dec, 2023, 19:48

No i have asked to fetch some docs for example posts from now to in the past

D5
18 Dec, 2023, 19:50

I understand. I thought it was to ban as said. In such case, you could get too with a function it. This seems an interesting request so I recommend to create a post in the appwrite GitHub

D5
18 Dec, 2023, 19:52

Edit: appwrite, not appetite. My phone autocorrect is so bugged πŸ˜†

Drake
18 Dec, 2023, 20:11

you don't need to get the server time

-N I N J A-
19 Dec, 2023, 05:54

Ok, for example I need posts list of past 1 week and for this small task small query is enough like

appwrite.listDocs(..., [ Query.greaterThan( 'created', new Date(appwrite.serverTimestamp().setDate(appwrite.serverTimestamp().getDate() - 7) ) ] );

why to create cloud function??

Drake
19 Dec, 2023, 05:55

Again, no need to get the server timestamp.

  1. Get the current timestamp
  2. Subtract 7 days
  3. Convert to iso 8601 in UTC
-N I N J A-
19 Dec, 2023, 05:56

On local device??

Drake
19 Dec, 2023, 05:56

Yes

-N I N J A-
19 Dec, 2023, 05:57

But user can manipulate it you can check it on your Android

Drake
19 Dec, 2023, 05:57

So what

-N I N J A-
19 Dec, 2023, 05:57

By Changing date of device also if I use utc

Drake
19 Dec, 2023, 06:00

Please don't randomly post like this

-N I N J A-
19 Dec, 2023, 06:10

User can access all of my database by manipulating date

-N I N J A-
19 Dec, 2023, 06:11

*device date

Drake
19 Dec, 2023, 06:19

If you really care about hiding data, you'll have to either:

  1. make the collection private and expose the data via a function
  2. Only make some documents readable
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