difference between DateProperty and DateTimeProper

2019-08-29 04:26发布

In layman's term, what's the difference between ndb.DateProperty and ndb.DateTimeProperty? When would I use which? For example, my intent is to use either with the param auto_now_add=True.

2条回答
SAY GOODBYE
2楼-- · 2019-08-29 04:37

From the docs:

These take values belonging to the corresponding classes (date, time, datetime) of the standard Python datetime module. The most general of the three is DateTimeProperty, which denotes both a calendar date and a time of day; the others are occasionally useful for special purposes requiring just a date (such as a date of birth) or just a time (such as a meeting time). For technical reasons, DateProperty and TimeProperty are subclasses of DateTimeProperty, but you shouldn't depend on this inheritance relationship (and note that it differs from the inheritance relationships between the underlying classes defined by the datetime module itself).

As expected, one is used if you don't need any reference to the time (eg: just the date), the other when you want the full information. When you would use one or the other completely depends on what information you need at a very precise moment.

查看更多
3楼-- · 2019-08-29 04:56

I would say that DateProperty is when you want a date-part only (i.e. dd/mm/yyyy or whatever format), and DateTimeProperty is useful when you want a full date & time representation (i.e. dd/mm/yyyy 00:00:00 or whatever format).

查看更多
登录 后发表回答