Skip to main content

query Complex Time Queries

In the WeDa low-code platform, time queries are a common and important database operation. This article will introduce how to use query in the WeDa Editor to query data based on time conditions.

Time Field Type

Common time type fields typically include the following:

  • Date Type: Used to store dates in the format YYYY-MM-DD
  • DateTime Type: Used to store date and time in the format YYYY-MM-DD HH:MM:SS
  • Timestamp Type: Stores timestamps, representing the number of milliseconds from 1970-01-01 00:00:00 UTC to the specified time.

In WeDa data models, Timestamp is primarily used to store time-type fields.

Common Time Query Scenarios

1. Query data for the current day or specific dates

Query today's data


// Greater than or equal to start time
new Date().setHours(0, 0, 0, 0)
// Less than or equal to end time
new Date().setHours(23, 59, 59, 59)

To query data for specific dates, you can filter by start time, for example:


// Greater than or equal to start time
new Date("2024-05-16").setHours(0, 0, 0, 0)
// Less than or equal to end time
new Date("2024-05-16").setHours(23, 59, 59, 59)

query-time-select

2. Query data within a date range

To query data within a specific date range, you can use the greater than or equal to (>=) and less than or equal to (<=) operators. Refer to the steps above for the query approach.


// Greater than or equal to start time
new Date("2025-xx-xx").setHours(0, 0, 0, 0)
// Less than or equal to end time
new Date("2025-yy-yy").setHours(23, 59, 59, 59)

3. Query Recent Data

To query data for a recent period (e.g., the last 15 days), you can dynamically calculate the time:


// Last 15 days
(new Date().getTime()) - 3600 * 24 * 15 * 1000

4. Query by current month, specified month, or year

Query by the start time of the current month


// 00:00:00 on the first day of the current month (local time zone)
new Date(new Date().getFullYear(), new Date().getMonth(), 1).getTime();

// 23:59:59.999 on the last day of the current month (local time zone)
new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0, 23, 59, 59, 999).getTime()

Query by the start time of the specified year and month


const year = 2025;
const month = 09; // October

// 00:00:00 on the first day of the specified month (local time zone)
const monthStart = new Date(year, month - 1, 1).getTime();

// 23:59:59.999 on the last day of the specified month (local time zone)
const monthEnd = new Date(year, month, 0, 23, 59, 59, 999).getTime();

Frequently Asked Questions

Query results are unexpected

  • Check whether the time format is correct
  • Confirm the time zone configuration
  • Verify the logical relationships of query conditions

Query Performance Issues

  • Check whether appropriate indexes have been created
  • Optimize query conditions to avoid full table scans
  • Consider using pagination queries to reduce the amount of returned data

Summary

In the WeDa Editor, by using the Query component with appropriate time-based query conditions, you can flexibly implement data queries across various time dimensions. Mastering the time query techniques introduced in this article will help you develop data query features in WeDa applications more efficiently.