How datetime is handled in Django (Strftime/strptime)

Source: Internet
Author: User
This article mainly introduces the processing method of DateTime in Django (Strftime/strptime), this article introduces to you very detailed, has certain reference value, needs the friend can refer to under

strftime< convert Date,datetime,timezone.now () type processing to String type >

The Strftime () function is a function that formats a date, DateTime, and time, and supports classes such as date, date, time, and so on, which are represented as strings by format character requirements.

Import Datatimedatatime.datatime.now ()

Or

From datatime import Datatimedatatime.now ()

My Output Conversion format

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

Effect similar to 2018-07-02 23:18:20 so.

strptime< converting string processing to expected types of data >

The Strptime () function is the date time from the string representation converted to the appropriate datetime by the format string requirement.

D2 = Datetime.strptime (' 2018-03-02 17:41:20 ', '%y-%m-%d%h:%m:%s ')

Conversion Control Format:

Turn into a string

%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12) 12-hour system
%M minutes (00=59)
%s seconds (00-59)

Two bugs encountered:

One: Shows the time and database inconsistencies of the bug:

Once encountered such a problem, the time taken out from the database and the front-end display time is inconsistent. Later, after more than the output time of the conversion of information, and finally found that because the previous writing code of the colleague, used the

one[' time ' = one[' time '].strftime ('%y-%m-%d%h:%i:%s ')

The%i is a 12-hour conversion form. This conversion format, although not error, but the output will be inconsistent with the database, the database is recorded in 24 hours of time.

II: Datetime.datetime (2018, 2, 2, +, +, tzinfo=<utc>) is not JSON serializable

This error message appears:

Datetime.datetime (2018, 2, 2, tzinfo=<utc>) is not JSON serializable

This is the error message that appears in the JSON where the datetime data is placed directly. cannot be placed directly in JSON.

The workaround is: You can use Strftime to serialize, as in the following method

. Strftime ("%y-%m-%d%h:%m:%s")

Below is a look at Python:time, strftime and strptime specific explanations

The most commonly used Time.time () returns a floating-point number in seconds. But the type of strftime processing is time.struct_time, which is actually a tuple. Strptime and LocalTime will return this type.

>>> Import time>>> t = time.time () >>> t1202872416.4920001>>> type (t) <type ' Float ' >>>> t = time.localtime () >>> t (2, 2, 0) >>> type (t) <type ' Time.struct_time ' >>>> time.strftime ('%y-%m-%d ', t) ' 2008-02-13 ' >>> time.strptime (' 2008-02-14 ') , '%y-%m-%d ') (2008, 2, 14, 0, 0, 0, 3, 45,-1)

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.