如何在 SQLite 查询中将字符串转换为日期? (西瓜数据库)
How convert a string to date in SQLite query? (WatermelonDB)
我在我的应用程序中使用 watermelonDB,但是 watermelon 将日期保存为数据库中的数字。
我打算获取月份或年份的所有项目,但是我无法将过滤器的日期与数据库的日期进行比较。
`SELECT * FROM invoices WHERE
${byYear
? `date(${dateString}, 'start of year') = date(created_at, 'start of year')`
: `date(${dateString}, 'start of month') = date(created_at, 'start of month') AND
date(${dateString}, 'start of year') = datetime(created_at, 'start of year')`
}`
是否可以选择使用 SQLite 方法在“YYYY-MM-DD”中转换 created_at
?
似乎 table 中存储的时间戳是表示自 1970-01-01 00:00:00
.
以来的毫秒数的整数
您可以将它们转换为 YYYY-MM-DD
格式的可读文本日期:
date(created_at / 1000, 'unixepoch')
或
date(created_at / 1000, 'unixepoch', 'localtime')
除以 1000
去掉毫秒。
您也可以应用其他修饰符,例如 'start of year'
或 'start of month'
:
date(created_at / 1000, 'unixepoch', 'localtime', 'start of year')
我在我的应用程序中使用 watermelonDB,但是 watermelon 将日期保存为数据库中的数字。
我打算获取月份或年份的所有项目,但是我无法将过滤器的日期与数据库的日期进行比较。
`SELECT * FROM invoices WHERE
${byYear
? `date(${dateString}, 'start of year') = date(created_at, 'start of year')`
: `date(${dateString}, 'start of month') = date(created_at, 'start of month') AND
date(${dateString}, 'start of year') = datetime(created_at, 'start of year')`
}`
是否可以选择使用 SQLite 方法在“YYYY-MM-DD”中转换 created_at
?
似乎 table 中存储的时间戳是表示自 1970-01-01 00:00:00
.
您可以将它们转换为 YYYY-MM-DD
格式的可读文本日期:
date(created_at / 1000, 'unixepoch')
或
date(created_at / 1000, 'unixepoch', 'localtime')
除以 1000
去掉毫秒。
您也可以应用其他修饰符,例如 'start of year'
或 'start of month'
:
date(created_at / 1000, 'unixepoch', 'localtime', 'start of year')