The time module of the Python module

Source: Internet
Author: User
Tags local time timedelta

Import time

#从1970年1月1号凌晨开始到现在的秒数 because the first commercial version of UNIX was listed this year, the most common
# Print (Time.time ())

# 1491574950.2398355


#返回当前的系统时间, you can add parameters, such as the second example below
# Print (Time.ctime ())
# Print (Time.ctime (Time.time ()-86400))

# Fri APR 7 22:24:17 2017
# Thu APR 6 22:25:15 2017



#可以把年, month, day, hour and minute are displayed separately, but the time shown here is GMT, and the second example below can be used to make time string concatenation
# Print (Time.gmtime ())
# time_obj = Time.gmtime ()
# Print (Time_obj.tm_year,time_obj.tm_mon)

# Time.struct_time (tm_year=2017, tm_mon=4, tm_mday=7, tm_hour=14, tm_min=25, tm_sec=59, tm_wday=4, tm_yday=97, TM_ISDST =0)
# 2017 4


#上面显示的格林威治时间, here is the local time shown below
# Print (Time.localtime ())

# Time.struct_time (tm_year=2017, tm_mon=4, tm_mday=7, tm_hour=22, tm_min=31, tm_sec=42, tm_wday=4, tm_yday=97, TM_ISDST =0)


#把一个时间对象转换成时间戳

# Print (Time.mktime (time_obj))
# 1491546920.0


#等待10s
# Time.sleep (10)
# print (' Wait 10s ')


#将时间对象转换成指定的字符串格式, there are two parameters, the format and the time object, you can choose the time format you want, such as the need for the date, or the time, or to

# Print (Time.strftime ("%y-%m-%d:%h:%m:%s", Time.gmtime ()))
# 2017-04-07:14:40:28

# Print (Time.strftime ("%y-%m-%d:%h:%m:%s", Time.localtime ()))
# 2017-04-07:22:40:50


# ret = time.strptime (' 2016-12-23 15:34:34 ', '%y-%m-%d%h:%m:%s ')
# Print (ret)

# Time.struct_time (tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=15, tm_min=34, tm_sec=34, tm_wday=4, tm_yday=358, tm_ ISDST=-1)


Import datetime

#显示今天的日期
# Print (Datetime.date.today ())
# 2017-04-07


# ret = Datetime.datetime.now ()
# Print (ret)

# 2017-04-07 22:48:18.861383

#转换time的形式
# Print (Ret.timetuple ())

# Time.struct_time (tm_year=2017, tm_mon=4, tm_mday=7, tm_hour=22, tm_min=49, tm_sec=46, tm_wday=4, tm_yday=97, TM_ISDST =-1)

#下面介绍下时间的加减

#给当前的时间加10天
# Print (Datetime.datetime.now () + Datetime.timedelta (days=10))
# 2017-04-17 22:55:03.070503


#给当前时间减10天
# Print (Datetime.datetime.now () + Datetime.timedelta (days=-10))
# Print (Datetime.datetime.now ()-Datetime.timedelta (days=10))

# 2017-03-28 22:56:08.529247
# 2017-03-28 22:56:08.529247

#上面的例子是加减天, in fact Timedelta can support the addition and subtraction of the following parameters, such as weeks, hours, minutes
# days=0, Seconds=0, Microseconds=0,milliseconds=0, Minutes=0, Hours=0, weeks=0):


#
Current_time = Datetime.datetime.now ()
Print (Current_time)

#替换为某年, one month, one day
Print (Current_time.replace (2016,1,3))
Print (Current_time.replace (2016,3))
Print (Current_time.replace (2015))

# 2016-01-03 23:01:54.095012
# 2016-03-07 23:01:54.095012
# 2015-04-07 23:01:54.095012



#两个时间还可以做比较
# gtime = Datetime.datetime.now ()
# CTime = Gtime.replace (2015)
# if Gtime > CTime:
# print (' true ')
# Else:
# print (' false ')

The time module of the Python module

Related Article

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.