Python Date and time

Source: Internet
Author: User
Tags local time

Time.time () gets the current timestamp

For example:

#!/usr/bin/-*-coding:utf-8-*-import time ;   ="Current timestamp is:", ticks

Results:

The current timestamp is: 1491750320.74
Time tuple

For example, get the current time:

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

Results:

local time: Sun APR 09 23:12:15 2017
Formatted date

Format is a bit like Java date formatting

Import Time#formatted as 2016-03-20 11:45:39 in the form ofPrintTime.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())#formatted as Sat 28 22:24:24 2016 FormPrintTime.strftime ("%a%b%d%h:%m:%s%Y", Time.localtime ())#convert a format string to a timestampA ="Sat Mar 22:24:24"PrintTime.mktime (Time.strptime (A,"%a%b%d%h:%m:%s%Y"))

Results:

2017-04-09 23:13:50Sun Apr 09 23:13:50 20171459175064.0

Date formatting symbols

Get a calendar for a month

Display information for a month directly in the form of a calendar

Calendar for April 2017:

#!/usr/bin/python
#-*-Coding:utf-8-*-

Import Calendar

Calendar.month (2017, 4)
Print "The following output April 2017 calendar:"
Print Cal;

Show Results:

The following outputs the April 2017 calendar:
April 2017
Mo Tu We Th Fr Sa Su
0 S
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Time Module

Calendar Module

Python Date and time

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.