Home  >  Article  >  Backend Development  >  How to handle datetime in Django (strftime/strptime)

How to handle datetime in Django (strftime/strptime)

不言
不言Original
2018-07-03 15:01:202519browse

This article mainly introduces the processing method of datetime in Django (strftime/strptime). This article introduces you in very detail and has certain reference value. Friends in need can refer to it

strftimebceb92db6a9c14802a698958b31b8ed4

The strftime() function is a function used to format a date, date time and time. Supports date, datetime, time and other classes, and formats these times as strings through format characters.

import datatime
datatime.datatime.now()

or

from datatime import datatime
datatime.now()

My output conversion format

strftime('%Y-%m-%d %H:%I:%S')

The effect is similar to 2018-07-02 23:18:20.

strptime92281ede5e3fa6706627b7f07e17f74f

The strptime() function is to convert the date and time represented by a string into the corresponding date and time according to the format string requirements.

d2 = datetime.strptime('2018-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')

Format of conversion comparison:

Convert to string

%y Two-digit year representation (00-99)
%Y four Number of digits representing the year (000-9999)
%m Month (01-12)
%d Day in the month (0-31)
%H Number of hours in 24-hour format (0-23 )
%I Hours in 12-hour format (01-12) Twelve-hour format
%M Minutes (00=59)
%S Seconds (00-59)

Two bugs encountered:

One: The display time is inconsistent with the database bug:

Once I encountered such a problem, the time taken out from the database was inconsistent with the time displayed on the front end. Later, after outputting more information and converting the time, I finally found out that the colleague who wrote the code before used this

one['time'] = one['time'].strftime('%Y-%m-%d %H:%I:%S')

%I is the conversion form of the twelve-hour clock. When converting the format in this way, although no error will be reported, the output result will be inconsistent with the database. The database records time in 24-hour units.

Two: datetime.datetime(2018, 2, 2, 18, 25, 29, tzinfo=e9565f3311cfba241e510fbff062dc7c) is not JSON serializable

Such an error message appears ..

datetime.datetime(2018, 2, 2, 18, 25, 29, tzinfo=<UTC>) is not JSON serializable

This is the error message that appears when datetime data is placed directly in json. It cannot be placed directly in json.

The solution is: you can use strftime for serialization, Such as the following method

.strftime("%Y-%m-%d %H:%M:%S")

Let’s look at the detailed explanation of Python: time, strftime and strptime

The most commonly used time.time() What is returned is a floating point number in seconds. But the type processed by strftime is time.struct_time, which is actually a tuple. Both strptime and localtime will return this type.

>>> import time
>>> t = time.time()
>>> t
1202872416.4920001
>>> type(t)
<type &#39;float&#39;>
>>> t = time.localtime()
>>> t
(2008, 2, 13, 10, 56, 44, 2, 44, 0)
>>> type(t)
<type &#39;time.struct_time&#39;>
>>> time.strftime(&#39;%Y-%m-%d&#39;, t)
&#39;2008-02-13&#39;
>>> time.strptime(&#39;2008-02-14&#39;, &#39;%Y-%m-%d&#39;)
(2008, 2, 14, 0, 0, 0, 3, 45, -1)

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

ThinkPHP framework uses redirect to implement page redirection method example explanation

php framework CodeIgniter uses redis The method is explained

The above is the detailed content of How to handle datetime in Django (strftime/strptime). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn