Obtain the Host Name and current time in Python

Source: Internet
Author: User

1. Get the Host Name

Import socket

Hostname = socket. gethostname ()


2. Get the current time

Import time

Print time. Time ()

The timestamp of the current time, such as 1312458498.031.


Format the timestamp as local time using the time. localtime () method.

Time. localtime (Time. Time ())

Output result:

Time. struct_time (tm_year = 2011, tm_mon = 8, tm_mday = 4, tm_hour = 19, tm_min = 50, tm_sec = 20, tm_wday = 3, tm_yday = 216, tm_isdst = 0)


Use the time. strftime () method to format it as the expected result:

Time. strftime ('% Y-% m-% d', time. localtime (Time. Time ()))

Output result:


Common time and date formatting symbols in Python:

% Y two-digit year (00-99)

% Y indicates the four-digit year (000-9999)

% M month (01-12)

One day in % d month (0-31)

% H hour in 24-hour format (0-23)

% I 12-hour (01-12)

% M minutes (00 = 59)

% S seconds (00-59)

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.