# Python DateTime Module

This Module provides sufficient features related to date and time. Its a quite useful module in python.

#### Basic Examples

```import datetime

print(datetime.datetime.today()) # This prints today's date with time

print(datetime.datetime.now()) # This prints present date with time with more precise than datetime.datetime.today()

print(datetime.datetime.min) #This prints initial date with time

print(datetime.datetime.max) #This prints max date with time

print(datetime.datetime.today().year) #This prints year of date object

print(datetime.datetime.today().month) #This prints month of date object

print(datetime.datetime.today().day) #This prints day of date object

print(datetime.date(2010,12,15))
#-->> 2010-12-15

#PYTHON OUTPUT
2019-01-22 17:18:21.711971
2019-01-22 17:18:21.712027
0001-01-01 00:00:00
9999-12-31 23:59:59.999999
2019
1
22
2010-12-15
```

#### dateTime.date() syntax

`datetime.date(YEAR, MONTH, DAY)`

#### For string representation of Date-Time use `strftime()` method this returns date object as string.

```import datetime

now = datetime.datetime.now()

print(now.strftime("%d")) # This prints Day
print(now.strftime("%B")) # This prints Month's fullname
print(now.strftime("%Y")) # This prints full year

print(format(now.strftime("%d-%B-%Y")))
print("Today's date is: {}".format(now.strftime("%d-%B-%Y")))

#PYTHON OUTPUT
22
January
2019
22-January-2019
Today's date is: 22-January-2019
```

#### Convert String to time

```import datetime

date="2018-12-11"
formatted_date=datetime.datetime.strptime(date, "%Y-%m-%d")

print(formatted_date)

#PYTHON OUTPUT
2018-12-11 00:00:00
```

## Difference between dates

Finding a difference in dates using date object

```import datetime

f_date = datetime.date(2008, 8, 18)
t_date = datetime.date(2010, 8, 18)
diff_date = t_date-f_date

print(diff_date)

#PYTHON OUTPUT
730 days, 0:00:00
```

The above method gives use difference in terms of days. but what if we want the difference in hours, minutes, months or years.
Then we need to use python DateTime modules `strptime()` method. This will convert the string to a python DateTime object.

```from datetime import datetime

format = '%Y-%m-%d %H:%M:%S.%f'
date1 = '2008-02-01 01:01:01.001'
date2 = '2009-02-01 02:02:02.002'

f_date = datetime.strptime(date2, format)

t_date = datetime.strptime(date1, format)

date_diff = f_date-t_date
print("Date Difference: {}".format(date_diff))
print("Difference in Days: {} ".format(date_diff.days))
print("Difference in Microseconds: {} ".format(date_diff.microseconds))
print("Difference in Seconds: {} ".format(date_diff.seconds))

#PYTHON OUTPUT
Date Difference: 366 days, 1:01:01.001000
Difference in Days: 366
Difference in Microseconds: 1000
Difference in Seconds: 3661
```

### Working with Python time module

This module is helpful in working with different time-related methods, timezone conversion and time manipulation.

#### Below describe some of the useful methods in `time` Module

Getting current time using the time module.

```from time import gmtime, strftime

print(gmtime())

#PYTHON OUTPUT
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=25, tm_hour=9, tm_min=39, tm_sec=56, tm_wday=4, tm_yday=25, tm_isdst=0)

```

`gmtime()` the method will print time in tuple form to convert this to readable string use `strftime()` method.

```from time import gmtime, strftime

print(gmtime())
print(strftime("%I:%M:%S %p",gmtime()))

#PYTHON OUTPUT
09:39:56 AM
```

Note: `gmtime()` method may not give precise time insisted we can use `localtime()` method for time accuracy.

```from time import gmtime, strftime, localtime
print(strftime("%I:%M:%S %p",localtime()))

#PYTHON OUTPUT
03:14:00 PM
```

Creating a time object takes two arguments in `strptime()` method. This method converts time string to time object.

```import time

new_time=time.strptime("12 december 2016 09:24:58 AM", "%d %B %Y %H:%M:%S %p")

print(new_time)
print(time.strftime("%I:%M:%S %p",new_time))

#PYTHON OUTPUT
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=12, tm_hour=9, tm_min=24, tm_sec=58, tm_wday=0, tm_yday=347, tm_isdst=-1)
09:24:58 AM
```

For additional information of using DateTime, you can refer our article on DateTime module with programs and also link to python documentation provided.
{"ft_lang_label":"__label__en","ft_lang_prob":0.54375494,"math_prob":0.90041983,"size":4125,"snap":"2021-31-2021-39","text_gpt3_token_len":1100,"char_repetition_ratio":0.1878185,"word_repetition_ratio":0.004158004,"special_character_ratio":0.31369698,"punctuation_ratio":0.19001219,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9856003,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-27T15:36:50Z\",\"WARC-Record-ID\":\"<urn:uuid:f3b75df3-5945-4499-b221-39b2fab1982e>\",\"Content-Length\":\"66388\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8fdcbfbd-cb68-45d3-99f2-30138e18f14f>\",\"WARC-Concurrent-To\":\"<urn:uuid:e33fbf8e-d805-4a02-b602-cc38eaa488ae>\",\"WARC-IP-Address\":\"166.62.28.97\",\"WARC-Target-URI\":\"https://thecodelearners.com/python-datetime-module/\",\"WARC-Payload-Digest\":\"sha1:GMMIKATTSAMQY2JAYOONMT3DPAVXHTZ5\",\"WARC-Block-Digest\":\"sha1:YTLC4LGMV34W2VCAU7YIHSLACC3SC23W\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780058456.86_warc_CC-MAIN-20210927151238-20210927181238-00470.warc.gz\"}"}