python 中date datetime time 與str的互轉

  如下所有引入python

  form datetime import datetime, timedeltaapp

  import timecode

1、time 轉strorm

  

 

 

2、datetime 轉 strblog

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

 

3、str 轉 datetimeget

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

4、實例應用form

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--class

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

相關文章
相關標籤/搜索