вторник, 10 ноября 2015 г.

№3 Библиотека datetime

Очевидно из названия, для чего она используется.

Видимо, так мы подключаем пакеты необходимые нам.

from datetime import datetime
The first line imports the datetimelibrary so that we can use it.
We can use a function calleddatetime.now() to retrieve the current date and time.
Extracting Information
Notice how the output looks like 2013-11-25 23:45:14.317454. What if you don't want the entire date and time?
from datetime import datetime
now = datetime.now()

current_year = now.year
current_month = now.month
current_day = now.day
You already have the first two lines.
In the third line, we take the year (and only the year) from the variable nowand store it in current_year.

Чтобы поиграться с форматами вывода данных воспользуемся волшебным опрератором  %(оператором же или кто он?) %s*, где на месте *будет стоять любой символ, который будет разделять данные
from datetime import datetime
now = datetime.now()
print '%s/%s/%s' % ( now.month, now.day, now.year)
11/10/2015

Тоже самое можно делать и с часами, минутами и секундами: 
from datetime import datetime
now = datetime.now()

print '%s:%s:%s' % ( now.hour, now.minute, now.second)

print '%s/%s/%s %s:%s:%s' % ( now.month, now.day, now.year,now.hour, now.minute, now.second)
Выводит: 
11/10/2015 2:38

Комментариев нет:

Отправить комментарий