How to handle datetime in Django (strftime/strptime)

不言
Release: 2023-04-02 08:46:01
Original
2576 people have browsed it

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

strftime

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()
Copy after login

or

from datatime import datatime
datatime.now()
Copy after login

My output conversion format

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

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

strptime

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')
Copy after login

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')
Copy after login

%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=<UTC>) is not JSON serializable

Such an error message appears ..

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

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")
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template