The method of converting Python's normal time and Unix timestamp to each other _python

Source: Internet
Author: User

The examples in this article describe the ways in which Python's normal time and Unix timestamp are converted to each other. Share to everyone for your reference. The specific analysis is as follows:

This code can be used to convert a regular time format to a Unix timestamp, or to convert a Unix timestamp back,
For example: 1332888820 format converted to 2012-03-28 06:53:40 form

 #-*-Coding:utf-8-*-import Time def timestamp_datetime (value): Format = '%y-%m-%d%H: %m:%s ' # value is the passed-in value for the timestamp (shaping), such as: 1332888820 value = Time.localtime (value) # # after localtime converted to # # time.struct_time (tm_year =2012, Tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0) # finally go through the strftime function into normal day
 Period format. DT = time.strftime (format, value) return dt def datetime_timestamp (DT): #dt为字符串 #中间过程, it is generally necessary to convert a string into a time array time.strptim E (DT, '%y-%m-%d%h:%m:%s ') # # Time.struct_time (tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, Tm_w
  day=2, tm_yday=88, tm_isdst=-1) #将 "2012-03-28 06:53:40" converted to timestamp s = time.mktime (time.strptime (DT, '%y-%m-%d%h:%m:%s ')) return int (s) if __name__ = = ' __main__ ': D = datetime_timestamp (' 2012-03-28 06:53:40 ') print d s = timestamp_datetime (1332888820) print S 

PS: Here again for you to recommend a local UNIX timestamp conversion tool, with a variety of languages (python/php/java/mysql, etc.) Unix timestamp operation methods:

Unix Timestamp (timestamp) conversion tool:Http://tools.jb51.net/code/unixtime

I hope this article will help you with your Python programming.

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.