Python Time conversion Application __python

Source: Internet
Author: User
Tags timedelta

Import time,datetime from
datetime import timedelta
import sys
reload (SYS)

format= "%y-%m-%d%h:%m:%s "Now
= Datetime.datetime.now (). Replace (hour=0,minute=0,second=0)
Aday = Timedelta (days =-1)
enddate= Now+aday
Startdate=enddate.replace (day=1);
Print now.strftime (format) print enddate.strftime (format) print
startdate.strftime (format)
print int (Time.mktime (Time.strptime (enddate.strftime (format), '%y-%m-%d%h:%m:%s ')) *1000)
Print Time.strftime (Format,now)

(1) When using Python, it often involves the operation of time.

The most commonly used time module in Python.

Import time,datetime from
datetime import timedelta
import sys
reload (SYS)

Get current time

format= "%y-%m-%d%h:%m:%s"

Now=datetime.datetime.now ()//Current time

Print now

#2013-11-07 14:59:53.331000

Set year, month, day

Now.replace (year=2011,month=12,day=1,hour=0,minute=0,second=0)

Converts a date (now) to a string

Now.strftime (format)//mode 1

Get current time stamp

Print Time.time ();

1383807657.91

Convert a string to a date

Time.strptime (Format,now)//Mode 2

Converts a string to a timestamp

Time.mktime (Time.strptime (' 2012-03-28 06:53:40 ', '%y-%m-%d%h:%m:%s '))

Convert time stamp to date

Time.localtime (1317091800.0)
Time.strftime ('%y-%m-%d%h:%m:%s ', x)




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.