jueves, 8 de agosto de 2024

Working with datetime time and datetime object

>>> datetime.datetime.now()

datetime.datetime(2024, 8, 11, 21, 12, 36, 860592)

>>> datetime.datetime.now().time()

datetime.time(21, 12, 41, 511824)

>>> datetime.datetime.now().date()

datetime.date(2024, 8, 11)


>>> datetime.datetime.now()

datetime.datetime(2024, 8, 10, 11, 30, 45, 554555)


>>> datetime.datetime.now().year

2024

>>> datetime.datetime.now().month

8

>>> datetime.datetime.now().day

10


 Creating datetime.time object

time=datetime.time(23,59)

Getting hour

time.hour

23

Getting minutes   


time.minute

59

----------

Getting current datetime and time object

>>> datetime.datetime.now()
datetime.datetime(2024, 8, 8, 11, 37, 6, 478499)

Getting time  object

datetime.datetime.now().time()
datetime.time(11, 37, 34, 900228)

Getting hour

 datetime.datetime.now().time().hour

11

Minute
tetime.datetime.now().time().minute
39


Getting formated time object from the time object

datetime.time(23,59).strftime('%H:%S:%p')

'23:00:PM'


getting  same  hour and minute   but  from now date time object


datetime.datetime.now().strftime('%H:%S:%p')

'11:17:AM'


Creating an str list with time

>>> time= str(datetime.datetime.now().time()).split(':')

>>> time

['21', '03', '06.749331']


Other example

>>> datetime.datetime.now()
datetime.datetime(2024, 8, 11, 21, 7, 35, 295040)

>>> str(datetime.datetime.now())
'2024-08-11 21:07:46.766833'

>>> str(datetime.datetime.now()).split(" ")
['2024-08-11', '21:08:23.892977']

>>> str(datetime.datetime.now()).split(" ")[0]
'2024-08-11'

Calculating the missing days for dic


>>> dic=datetime.datetime(2024, 12, 1, 0, 0)

>>> dic-datetime.datetime.now()

datetime.timedelta(days=111, seconds=7993, microseconds=258309)


https://www.programiz.com/python-programming/datetime/strftime

Format Code List

The table below shows all the codes that you can pass to the strftime() method.

DirectiveMeaningExample
%aAbbreviated weekday name.Sun, Mon, ...
%AFull weekday name.Sunday, Monday, ...
%wWeekday as a decimal number.0, 1, ..., 6
%dDay of the month as a zero-padded decimal.01, 02, ..., 31
%-dDay of the month as a decimal number.1, 2, ..., 30
%bAbbreviated month name.Jan, Feb, ..., Dec
%BFull month name.January, February, ...
%mMonth as a zero-padded decimal number.01, 02, ..., 12
%-mMonth as a decimal number.1, 2, ..., 12
%yYear without century as a zero-padded decimal number.00, 01, ..., 99
%-yYear without century as a decimal number.0, 1, ..., 99
%YYear with century as a decimal number.2013, 2019 etc.
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23
%-HHour (24-hour clock) as a decimal number.0, 1, ..., 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12
%-IHour (12-hour clock) as a decimal number.1, 2, ... 12
%pLocale’s AM or PM.AM, PM
%MMinute as a zero-padded decimal number.00, 01, ..., 59
%-MMinute as a decimal number.0, 1, ..., 59
%SSecond as a zero-padded decimal number.00, 01, ..., 59
%-SSecond as a decimal number.0, 1, ..., 59
%fMicrosecond as a decimal number, zero-padded on the left.000000 - 999999
%zUTC offset in the form +HHMM or -HHMM. 
%ZTime zone name. 
%jDay of the year as a zero-padded decimal number.001, 002, ..., 366
%-jDay of the year as a decimal number.1, 2, ..., 366
%UWeek number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, ..., 53
%WWeek number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.00, 01, ..., 53
%cLocale’s appropriate date and time representation.Mon Sep 30 07:06:05 2013
%xLocale’s appropriate date representation.09/30/13
%XLocale’s appropriate time representation.07:06:05
%%A literal '%' character.%




No hay comentarios:

Publicar un comentario