Home >Backend Development >Python Tutorial >How to format time? Python current time formatting operation display

How to format time? Python current time formatting operation display

乌拉乌拉~
乌拉乌拉~Original
2018-08-14 17:12:292209browse

In today’s article, let’s take a look at the part about python time formatting in python. python formattingThe date and time in this aspect are because of the There are relatively few aspects, so there may be relatively little research on this aspect. Today we will talk about one aspect of python date formatting.

Format date: We can use the strftime method of the time module to format the date. The specific code is as follows:

time.strftime(format[, t])

It doesn’t seem difficult , then let’s try this code.

Put him into the following example to try:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import time
 
# 格式化成2016-03-20 11:45:39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
 
# 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 
  
# 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

Let’s see the answer after input:

2018-08-14 17:05:32
Tue Aug 14 17:05:32 2018
1459175064.0

Looks good It has achieved the expected effect, and interested friends can try it themselves. Check to see if the time output is your current time.

In this article, we learned how to format our current time. In fact, this operation is quite simple. If you don’t understand, you can actually try it yourself. After all, hands-on practice is the best way to verify what you have learned. Finally, I hope this article can bring some help to you who are learning python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of How to format time? Python current time formatting operation display. 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