DateTime common Time processing method in Python

Source: Internet
Author: User
Common time conversion and processing functions:

Import datetime# Get current time D1 = Datetime.datetime.now () print d1# current time plus half hour d2 = D1 + Datetime.timedelta (hours=0.5) Print d2# format String Output D3 = D2.strftime ('%y-%m-%d%h:%m:%s ') print d3# convert string to time type D4 = Datetime.datetime.strptime (date, '%y-%m-%d%h:%m:%s .%f ') print D4

Get dates for this week and the first day of the month:

#-*-Coding:utf-8-*-import datetimedef first_day_of_month ():  "  get first day of the month  : return:  '  # Now_ Date = Datetime.datetime.now ()  # return (now_date + datetime.timedelta (days=-now_date.day + 1)). Replace (hour=0, Minute=0, Second=0,  # microsecond=0)  return Datetime.date.today ()-Datetime.timedelta (days= Datetime.datetime.now (). day-1) def first_day_of_week ():  ' Get the  first day of the week  : return:  ' '  return Datetime.date.today ()-Datetime.timedelta (Days=datetime.date.today (). Weekday ()) If __name__ = = "__main__":  This_week = First_day_of_week ()  Last_week = This_week-datetime.timedelta (days=7)  this_month = first_day_of _month ()  last_month = This_month-datetime.timedelta (days= (This_month-datetime.timedelta (Days=1)). Day)  Print This_week  print last_week  print this_month  print Last_month

#! /usr/bin/python# coding=utf-8import datetime "" "datetime is powerful enough to support 01 to 9999" "" "" "" Current Time Return is a DateTime type now method has a parameter tz, Set the time zone type. If you do not have the same effect as the method Today "" Now = Datetime.datetime.now () #UTC时间datetime. Datetime.utcnow () Attrs = [("Year", "Years"), (' Month ', "monthly "), (" Day "," Days "), (' Hour '," hours "), (' Minute '," Min "), (' second '," SEC "), (' microsecond '," millisecond "), (' Min '," min "), (' Max '," Max "),]for K,v in Attrs:  "now.%s =%s #%s"% (K,getattr (now, K), V) "" "Returns a time Structure" "" Now.timetuple () "" Returns a date Type "" "Now.date ()" " "Returns a Time Type" "" "Now.time ()" "" The current day of the week. Monday was 0, the week thereupon 6 note here is the method, not the attribute Oh. "" "Now.weekday ()" "" Current day of the week. Monday was 1, the week thereupon 7 note here is the method, not the attribute Oh. "" "Now.isoweekday ()" "" modifies the current time. For example, modified into the month of 1th "" "Now.replace (day=1) past = Datetime.datetime (2010,11,12,13,14,15,16)" "" for the comparison operation returned is the Timedelta type "" " Now-past "" "to string detail rule see time" "" Strdatetime = Now.strftime ("%y-%m-%d%h:%m:%s") "" "String Generate DateTime Object" "" Datetime.datetime.strptime (Strdatetime, "%y-%m-%d%h:%m:%s")

The above mentioned is the whole content of this article, I hope you can like.

  • 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.