Python obtains the current time

Source: Internet
Author: User
Tags month name ranges

Obtain the current time and output it using a string.

Format: % Y-% m-% d % H: % M: % s'

Datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s ')

Obtains the current time, but only retains the date.

Datetime. datetime. now (). date ()

Convert string to datetime type

Input string format: '% Y-% m-% d'

Datetime. datetime. strptime (time, '% Y-% m-% D ')

Print 'start at: ', datetime. datetime. now (). strftime (' % Y-% m-% d % H: % M: % S. % F ')
Print 'start at: ', time. strftime ('% Y-% m-% d % H: % M: % S. % F', time. localtime (time. time ()))

Appendix:

Formatted symbol Summary
% A: Weekday name, abbr.
% A full name of the day of the week
Abbreviated Month name, abbr.
% B full name of Month name
% C standard date string Complete date and time representation
% D Day of the month in decimal format
% H Hour in the 24-hour format (24-Hour clock)
% I Hour hour in 12-Hour format (12-hour clock)
% J Day of the year in decimal format
Month number in % m decimal format
% M decimal representation of minutes Minute number
% S decimal Second number
% U indicates the Week of the year. Sunday is regarded as the first day (value ranges from 0 to 53) Week number (Sunday first weekday)
% W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0) weekday number
% W indicates the Week of the year. Monday is regarded as the first day (from 0 to 53) Week number (Monday first weekday)
% X standard date string Complete date representation (e.g. 13/01/08)
% X standard time string Complete time representation (e.g. 17:02:10)
% Y decimal Year without century (value ranges from 0 to 99) Year number within century
% Y indicates the tenth Year number of the century section.
% Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned. Name of time zone
% Percent sign


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.