#AstroDB date format

7 messages · Page 1 of 1 (latest)

golden mauve
#

I have a date field in my Cron table. It's unclear to me if I need to convert this into a Date after loading for date math. Is this correct?

export const poorMansCron = async () => {
// Call crontask by API if more than 5 minutes since last call
const lastcall = (await db.select().from(Cron).where(eq(Cron.task, 'cronjob')))[0];
const fiveMinutes = 5 * 60 * 1000; // 5 minutes in milliseconds

if (!lastcall) {
// If no last call time is recorded, insert a new record
await db.insert().into(Cron).values({ task: 'cronjob', time: new Date() });
await crontasks();
} else if (lastcall && (new Date() - new Date(lastcall.time) > fiveMinutes)) {
// If more than 5 minutes have passed since the last call
await crontasks();
// Update the time after tasks are done
await db.update(Cron).set({ time: new Date() }).where(eq(Cron.task, 'cronjob'));
}
}

solar joltBOT
#

Hi @golden mauve. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you are correct. In Astro DB, date/time values are queried as JavaScript Date objects. This means that when you retrieve a date from your database, it will already be in the JavaScript Date format, and you can perform date math on it directly without needing to convert it first.

Here is the relevant extract from the Astro DB documentation:

Astro DB supports the following column types:
db/config.ts
import { defineTable, column } from 'astro:db';
const Comment = defineTable({ columns: { 
  // Date/time values queried as JavaScript Date objects.
  published: column.date(), 
}});
See the **table columns reference** (<https://docs.astro.build/en/guides/integrations-guide/db/#table-configuration-reference>) for more details.

In your case, lastcall.time should already be a JavaScript Date object, and you can subtract it from the current date to get the elapsed time in milliseconds. If this value is greater than fiveMinutes, then more than 5 minutes have passed since the last call.

Source: Astro DB Documentation (https://docs.astro.build/en/guides/astro-db#columns)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@golden mauve kapa.ai is still learning and improving, please let us know how it did by reacting below

golden mauve
#

@solar jolt So then this line was actually wrong because it is trying to convert the returned date value into a Javascript Date object?

} else if (lastcall && (new Date() - new Date(lastcall.time) > fiveMinutes)) {

solar joltBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you are correct. Since lastcall.time is already a JavaScript Date object, you don't need to convert it again with new Date(lastcall.time). You can directly subtract it from the current date. The correct line should be:

} else if (lastcall && (new Date() - lastcall.time > fiveMinutes)) {

This will correctly calculate the time difference in milliseconds between the current time and the time of the last call.

Source: Astro DB Documentation (https://docs.astro.build/en/guides/astro-db#columns)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: