Python syntax------time function

Source: Internet
Author: User
Tags month name

1. Import the function library:

Import time

Get the formatted time

You can choose a variety of formats according to your needs, but the simplest function to get a readable time pattern is asctime ():

#!/usr/bin/python#-*-coding:utf-8-*-Import= time.  Asctime( time.  LocalTime(time.  ())print"local time:", localtime        

Formatted date
# Formatted as 2016-03-20 11:45:39 formPrintTime.Strftime("%y-%m-%d%h:%m:%s",Time.LocalTime()) # formatted as Sat 28 22:24:24 2016 formprint Time. ( "%a%b%d%h:%m:%s%Y" , Timelocaltime ())  # convert format string to timestamp a =  "Sat Mar 28 22:24:24 2016" print Time. (time. Strptime (a, "%a%b%d%h:%m:%s%Y"               

Python format symbols in time Date:

    • %y Two-digit year representation (00-99)
    • %Y Four-digit year representation (000-9999)
    • %m Month (01-12)
    • One day in%d months (0-31)
    • %H 24-hour hours (0-23)
    • %I 12-hour hours (01-12)
    • %M minutes (00=59)
    • %s seconds (00-59)
    • %a Local Simplified Week name
    • %A Local Full week name
    • %b a locally simplified month name
    • %B Local Full month name
    • %c Local corresponding date representation and time representation
    • %j Day of the Year (001-366)
    • %p the equivalent of a local a.m. or p.m.
    • %u weeks of the year (00-53) Sunday is the beginning of the week
    • %w Week (0-6), Sunday for the beginning of the week
    • %W Week of the Year (00-53) Monday is the beginning of the week
    • %x Local corresponding date representation
    • %x Local corresponding time representation
    • %Z the name of the current time zone
    • Percent% of the number itself
    • TimeZone: Time.timezone offset seconds from Greenwich (>0, Americas; <=0 most of Europe, Asia, Africa).
    • Time.tzname
      The Time.tzname property contains a pair of different strings depending on the situation, which is the local time zone name with daylight savings, and No.
Get a calendar for a month
Import Calendar
Import Calendar
Cal =calendar.month (2016,6,1) #某月的日历, the following 2 parameters are optional
Print Calendar.isleap #闰年
Print Calendar.firstweekday () #返回当前每周起始日期的设置
Print Calendar.weekday (2016,6,12) #当前日期是日期码, Monday was 0

Python syntax------time function

Related Article

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.