python 中date datetime time 与str的互转


  以下全部引入

  form datetime import datetime, timedelta

  import time

一、time 转str

  

 

 

二、datetime 转 str

  str_date = datetime.now().strftime("%Y-%m-%d")   -------(%04d%02d%02d)此种格式化注意

 

三、str 转 datetime

  start_date = datetime.strptime("2016-06-07", "%Y-%m-%d")  

四、实例应用

def datelist(start, end):
    start_date = datetime.strptime(start, "%Y-%m-%d")
    end_date = datetime.strptime(end, "%Y-%m-%d")
    result = []
    curr_date = start_date
    while curr_date != end_date:
        # result.append("%04d-%02d-%02d" % (curr_date.year, curr_date.month, curr_date.day))
        result.append(curr_date.strftime("%Y-%m-%d"))
        curr_date += timedelta(1)
    # result.append("%04d-%02d-%02d" % (curr_date.year, curr_date.month, curr_date.day))
    result.append(curr_date.strftime("%Y-%m-%d"))
    return result

if __name__ == "__main__":
    print datelist("2016-06-07", "2016-06-27") 

  

 

 

 

 

----------2016-6-29 15:51:03--

    source: 【1】python string to datetime datetime to string


					


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM