Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python time strftime() method

Python time strftime() method


Description

The Python time strftime() function formats time, returning local time as a readable string. The format is determined by the format parameter.

Syntax

strftime() method syntax:



time.strftime(format[, t])

Parameters

Return Value

Returns the local time as a readable string.

Description

Date and time formatting symbols in Python:

Example

The following example shows how to use the strftime() function:

Example

#!/usr/bin/python

-- coding: UTF-8 --

By importing the future package to be compatible with Python 3.x print

If you are using Python 3.x, you can remove this import line

from

future

import

print_function

from

datetime

import

datetime

now

=

datetime

.

now

(

)

current date and time

year

=

now.

strftime

(

"%Y"

)

print

(

"year:"

,

year

)

month

=

now.

strftime

(

"%m"

)

print

(

"month:"

,

month

)

day

=

now.

strftime

(

"%d"

)

print

(

"day:"

,

day

)

time

=

now.

strftime

(

"%H:%M:%S"

)

print

(

"time:"

,

time

)

date_time

=

now.

strftime

(

"%Y-%m-%d, %H:%M:%S"

)

print

(

"date and time:"

,

date_time

)

The output of the above example is:



year: 2020

month: 09

day: 25

time: 10:24:28

date and time: 2020-09-25, 10:24:28