python get current time usage

python get current time usage

  • First import the library: import datetime
  • Get the current date and time: now_time = datetime.datetime.now ()
  • Format to the date we want: strftime () adds 1 hour to the current time: add_hour = datetime.datetime.now () + datetime.timedelta (hours = 1) # Need to import timedelta library format "hour": now_hour = add_hour. strftime ('% H')
    • 比如:“2020-04-19”:datetime.datetime.now().strftime('%Y-%m-%d')
  • Three ways to exist time: time object, time string, time stamp .
    • String to datetime:
      • string = '2020-04-19 11:23:00'
      • time1 = datetime.datetime.strptime(string,'%Y-%m-%d %H:%M:%S')
      • print time1
      • 2020-04-19 11:23:00
    • Datetime to string:
      • time1_str = datetime.datetime.strftime(time1,'%Y-%m-%d %H:%M:%S')
      • time1_str
      • '2020-04-19 11:23:00'
    • Timestamp to time object:
      • time1 = time.localtime()
      • time1_str = datetime.datetime.fromtimestamp(time1)
  • Format parameters :
    • % a shorthand for day of the week
    • % A full name of the day of the week
    • % b shorthand for month
    • % B Full month name
    • % c standard date time string
    • % C last two digits of year
    • % d Decimal day of the month
    • % D month / day / year
    • % e in the two-character field, the day of the month in decimal notation
    • % F year-month-day
    • % g The last two digits of the year, using a week-based year
    • % G Yearly Year, using week-based year
    • % h abbreviated month name
    • % H 24-hour hour
    • % I 12-hour clock
    • % j Decimal day of the year
    • % m Decimal month
    • % M Minutes in ten hours
    • % n newline
    • % p Equivalent display of local AM or PM
    • % r 12 hours
    • % R displays hours and minutes: hh: mm
    • % S decimal seconds
    • % t horizontal tab
    • % T displays hours, minutes and seconds: hh: mm: ss
    • % u Day of the week, Monday is the first day (value from 0 to 6, Monday is 0)
    • % U Week of the first year, with Sunday as the first day (value from 0 to 53)
    • % V Week of each year, using week-based year
    • % w Day of the week in decimal (value from 0 to 6, Sunday is 0)
    • % W Week of the year, with Monday as the first day (value from 0 to 53)
    • % x standard date string
    • % X standard time string
    • % y decimal year without century (value from 0 to 99)
    • % Y Ten-year year with century part
    • % z,% Z time zone name, or null character if no time zone name can be obtained.
    • %% percent sign

--------------------------------------------------------------------------------------

Reprinted: https://www.cnblogs.com/xiaoxiaoweng/p/10966220.html

 

Guess you like

Origin www.cnblogs.com/a001ai-es/p/12733384.html