ZZ Python datetime/time Conversions

Source: Internet
Author: User
Tags local time

Python Datetime/time conversionsdate:2008-11-12 |   Tags:datetime, Python | 3 Comments
FromDatetimeImportDatetime
ImportTime

#-------------------------------------------------
# Conversions to Strings
#-------------------------------------------------
# DateTime object to String
Dt_obj=Datetime(2008,11,10,17,53,59)
Date_str=Dt_obj.Strftime("%y-%m-%d%h:%m:%s ")
PrintDate_str

# time tuple to string
Time_tuple=(2008,11,12,13,51,18,2,317,0)
Date_str=Time.Strftime("%y-%m-%d%h:%m:%s ",Time_tuple)
PrintDate_str

#-------------------------------------------------
# Conversions to datetime objects
#-------------------------------------------------
# time tuple to datetime object
Time_tuple=(2008,11,12,13,51,18,2,317,0)
Dt_obj=Datetime(*Time_tuple[0:6])
PrintRepr(Dt_obj)

# Date string to datetime object
Date_str="2008-11-10 17:53:59"
Dt_obj=Datetime.Strptime(Date_str,"%y-%m-%d%h:%m:%s ")
PrintRepr(Dt_obj)

# Timestamp to datetime object in local time
Timestamp=1226527167.595983
Dt_obj=Datetime.Fromtimestamp(Timestamp)
PrintRepr(Dt_obj)

# Timestamp to datetime object in UTC
Timestamp=1226527167.595983
Dt_obj=Datetime.Utcfromtimestamp(Timestamp)
PrintRepr(Dt_obj)

#-------------------------------------------------
# Conversions to time tuples
#-------------------------------------------------
# DateTime object to TIME tuple
Dt_obj=Datetime(2008,11,10,17,53,59)
Time_tuple=Dt_obj.Timetuple()
PrintRepr(time_tuple) # string to Time Tupledate_str = "2008-11-10 17:53:59" time_tuple = Time.strptime (Date_str, "%y-%m-%d%H:%M:%S ") print repr (time_tuple) # Timestamp to time, tuple in utctimestamp = 1226527167.595983time_tuple = Time.gmtime (timestamp) p Rint repr (time_tuple) # Timestamp to time tuple in local timetimestamp = 1226527167.595983time_tuple = Time.localtime (Times Tamp) print repr (time_tuple) #-------------------------------------------------# conversions to timestamps#--------- ----------------------------------------# time tuple in local time to Timestamptime_tuple = (2008, 11, 12, 13, 59, 27, 2, 317, 0) timestamp = time.mktime (time_tuple) print repr (timestamp) #-------------------------------------------------# results#-------------------------------------------------# 2008-11-10 17:53:59# 2008-11-12 13:51:18# Datetime.datetime (--------------) # Datetime.datetime (2008, 11, 12, 595983) # Datetime.datetime (2008, 11, 12, 21, 59, 27, 595983) # (2008, 11, 10, 17, 53, 59, 0, 315,-1) # (2008, 11, 10, 17, 53, 59, 0, 315,-1) # (2008, 11, 12, 21, 59, 27, 2, 317, 0) # (2008, 11, 12, 13, 59, 27, 2, 317, 0) # 1226527167.0

ZZ Python datetime/time Conversions

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.