如何在sqlite3中以年,月和日期格式保存记录 [英] How to save records in year, month and date format in sqlite3

查看:632
本文介绍了如何在sqlite3中以年,月和日期格式保存记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



我想在Django中获取某些日期范围的记录,我用谷歌搜索我是否可以得到一些解决方案但似乎没有任何工作。



保存在数据库中的日期时间是2018年5月7日,上午11:33我从html获得的日期格式是2018-05-01。由于日期格式不同,我无法获取范围内的记录。请帮忙。谢谢。



我尝试了什么:



Hello friends,

I am trying to fetch records of certain date range in Django, i googled to see if i can get some solution but nothing seems to work.

Date time saved in database is "May 7, 2018, 11:33 a.m." and the date format i get from html is in "2018-05-01". Because the date format is different, i am not able to fetch records in range. Please help. Thank you.

What I have tried:

q1 = request.POST.get('q1') #2018-05-01
q2 = request.POST.get('q2') #2018-05-10 

ModelClass.objects.filter(created_date__range=(q1, q2))

#created_date field has date in format - "May 7, 2018, 11:33 a.m."

推荐答案

日期时间不以May 7,2018,11:33 am格式存储在数据库中。它通过一些用于查询数据库的函数以这种方式返回格式。



SQLite知道三种不同的格式来指定日期和时间:

The datetime is not stored in the format "May 7, 2018, 11:33 a.m." in the database. It is returned formatted in that way by some function used to query the databse.

SQLite knows three different formats to specify dates and times:

TEXT as ISO8601 strings(YYYY-MM-DD HH:MM:SS.SSS)。

REAL作为朱利安日数,是公元前4714年11月24日格林威治中午以来的天数根据公历格里高利历。

INTEGER as Unix Time,自1970-01-01 00:00:00 UTC以来的秒数。

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

所以你必须使用这些格式之一(在你的情况下可能是TEXT格式)。还有帮助函数来格式化日期: SQLite查询语言:日期和时间函数 [ ^ ]。



您发布的日期是已经是ISO 8601格式。所以你的代码应该可以不加修改地工作。

So you have to use one of these formats (probably the TEXT format in your case). There are also helper functions to format dates: SQLite Query Language: Date And Time Functions[^].

Your posted dates are already in the ISO 8601 format. So your code should work without modifications.


这篇关于如何在sqlite3中以年,月和日期格式保存记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆