manipulating_dates_in_python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
manipulating_dates_in_python [2022/07/22 23:21] – raju | manipulating_dates_in_python [2024/02/06 05:22] (current) – raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Tasks ===== | ||
+ | * [[Add dates]] | ||
+ | * uses | strptime | ||
+ | * [[Convert string to date]] | ||
+ | |||
+ | ===== Dummy ===== | ||
+ | |||
+ | ==== date in isoformat ==== | ||
+ | |||
+ | < | ||
+ | from datetime import date | ||
+ | today = date.today().isoformat() | ||
+ | print(type(today)) | ||
+ | print(today) | ||
+ | |||
+ | <class ' | ||
+ | 2023-09-18 | ||
+ | </ | ||
+ | |||
+ | It also works on datetime objects and will give the time in ISO 8601 format. | ||
+ | < | ||
+ | from datetime import datetime | ||
+ | now = datetime.today().isoformat() | ||
+ | print(type(now)) | ||
+ | print(now) | ||
+ | |||
+ | <class ' | ||
+ | 2023-09-18T13: | ||
+ | </ | ||
+ | |||
+ | Ref:- https:// | ||
+ | |||
+ | ==== extract date from datetime ==== | ||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.10.6 | packaged by conda-forge | (main, Oct 24 2022, 16:02:16) [MSC v.1916 64 bit (AMD64)] | ||
+ | Type ' | ||
+ | IPython 8.4.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | from datetime import datetime | ||
+ | s = '24 October 2022' | ||
+ | dtt = datetime.strptime(s, | ||
+ | print(dtt) | ||
+ | 2022-10-24 00:00:00 | ||
+ | |||
+ | In [2]: | ||
+ | type(dtt) | ||
+ | Out[2]: | ||
+ | datetime.datetime | ||
+ | |||
+ | In [3]: | ||
+ | dt = datetime.strptime(s, | ||
+ | print(dt) | ||
+ | 2022-10-24 | ||
+ | |||
+ | In [4]: | ||
+ | type(dt) | ||
+ | Out[4]: | ||
+ | datetime.date | ||
+ | </ | ||
+ | |||
==== get today' | ==== get today' | ||
< | < | ||
Line 97: | Line 159: | ||
* http:// | * http:// | ||
+ | ==== print the names of months in a year ==== | ||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.11.4 | packaged by Anaconda, Inc. | (main, Jul 5 2023, 13:47:18) [MSC v.1916 64 bit (AMD64)] | ||
+ | IPython 8.12.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | import calendar | ||
+ | for m in calendar.month_name: | ||
+ | print(m) | ||
+ | |||
+ | |||
+ | January | ||
+ | February | ||
+ | March | ||
+ | April | ||
+ | May | ||
+ | June | ||
+ | July | ||
+ | August | ||
+ | September | ||
+ | October | ||
+ | November | ||
+ | December | ||
+ | </ | ||
manipulating_dates_in_python.1658532094.txt.gz · Last modified: 2022/07/22 23:21 by raju