Python string to date [duplicate] - python

This question already has answers here:
Python date string to date object
(9 answers)
Closed 5 years ago.
I'm a Python newbie and don't how to convert a Python 3.5x string
'2017-04-19 00:23'
into a date and time like
April 19, 2017 12:23 am
and even get individual units like
April
19
2017
12:23 am
or get day of week for 4/19/217
Wednesday

Use python datetime module, something like this :
from datetime import datetime
date_str = '2017-04-19 00:23'
date_obj = datetime.strptime(date_str, '%Y-%m-%d %H:%M')
# To get a particular part of the date in a particular format such as "Wednesday" for the "Datetime Object"
print(date_obj.strftime('%A'))
print(date_obj.strftime('%c'))
This will result in :
Wednesday
Wed Apr 19 00:23:00 2017
Check out the documentation.

Related

Convert spanish date into a pandas datetime object [duplicate]

This question already has answers here:
Parse date and change format from spanish
(2 answers)
Closed 1 year ago.
I want to convert this df from an object to a datetime object, "ABR" is the Spanish for "APR"
Initial date
final date
30 ABR 21
31 MAY 21
30 JUN 21
31 JUL 21
my code:
import locale
locale.setlocale(locale.LC_TIME, '')
a["Fecha Inicial"].apply(lambda x: dt.strptime(x, "%d %b %y"))
ValueError
ValueError: time data '30 ABR 21' does not match format '%d %b %y'
This can solve your issue
import pandas as pd
import datetime as dt
a = pd.DataFrame({"Fecha Inicial": ['30 ABR 21', '30 JUN 21']})
mapping_dict = {'ENERO':'JAN',
'FEB':'FEB',
'MARZO':'MAR',
'ABR':'APR',
'MAYO':'MAY',
'JUN':'JUN',
'JUL':'JUL',
'AGOSTO':'AUG',
'SEPT':'SEP',
'OCT':'OCT',
'NOV':'NOV',
'DIC':'DEC'
}
a["Fecha Inicial"].apply(lambda x: dt.datetime.strptime(x.replace(x.split(' ')[1],mapping_dict[x.split(' ')[1]].capitalize()), "%d %b %y"))
locale has some issue that affects the dt.datetime.strptime.

How to extract date from string in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have strings in following format:
Friday January 3 2020 16:40:57
Thursday January 2 2020 19:26:19
Sunday January 5 2020 01:24:55
Tuesday December 31 2019 17:31:42
What is the best way to convert them into python date and time?
You can use datetime.strptime:
from datetime import datetime
d = "Friday January 3 2020 16:40:57"
datetime_object = datetime.strptime(d, '%A %B %d %Y %H:%M:%S')
print(datetime_object)
You can use dateparser
Install:
$ pip install dateparser
Sample Code:
import dateparser
t1 = 'Friday January 3 2020 16:40:57'
t2 = 'Thursday January 2 2020 19:26:19'
t3 = 'Sunday January 5 2020 01:24:55'
t4 = 'Tuesday December 31 2019 17:31:42'
dt1 = dateparser.parse(t1)
dt2 = dateparser.parse(t2)
dt3 = dateparser.parse(t3)
dt4 = dateparser.parse(t4)
for dt in [dt1, dt2, dt3, dt4]:
print(dt)
Output:
2020-01-03 16:40:57
2020-01-02 19:26:19
2020-01-05 01:24:55
2019-12-31 17:31:42

converting GMT to stftime using python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 4 years ago.
I want to convert Wed, 14 Mar 2018 07:30:00 GMT to 2018/03/14 in python. I am getting unicode object has no attribute strftime error. Someone help me.
Use:
from datetime import datetime
date_given = 'Wed, 14 Mar 2018 07:30:00 GMT'
datetime.strptime(date_given, '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y/%m/%d')
You can read up below links for more details:
Python documentation for strptime: Python 2, Python 3
Python documentation for strftime: Python 2, Python 3

Python convert seconds to datetime date and time [duplicate]

This question already has answers here:
How to convert integer timestamp into a datetime
(3 answers)
Closed 5 years ago.
How do I convert an int like 1485714600 such that my result ends up being Monday, January 30, 2017 12:00:00 AM?
I've tried using datetime.datetime but it gives me results like '5 days, 13:23:07'
Like this?
>>> from datetime import datetime
>>> datetime.fromtimestamp(1485714600).strftime("%A, %B %d, %Y %I:%M:%S")
'Sunday, January 29, 2017 08:30:00'
What you describe here is a (Unix) timestamp (the number of seconds since January 1st, 1970). You can use:
datetime.datetime.fromtimestamp(1485714600)
This will generate:
>>> import datetime
>>> datetime.datetime.fromtimestamp(1485714600)
datetime.datetime(2017, 1, 29, 19, 30)
You can get the name of the day by using .strftime('%A'):
>>> datetime.datetime.fromtimestamp(1485714600).strftime('%A')
'Sunday'
Or you can call weekday() to obtain an integers between 0 and 6 (both inclusive) that maps thus from monday to sunday:
>>> datetime.datetime.fromtimestamp(1485714600).weekday()
6

How to get month interval using "datetime" in python? [duplicate]

This question already has answers here:
How to get the last day of the month?
(44 answers)
Closed 8 years ago.
As every month have different days in it, so i can't apply timedelta=30.
I want to get three variables
month_start,
month_end ,
month_days = month_end - month_start
Which will be correspond to start date of month and end date of month. and their interval will be number of days in the month.
for instance , for march : month_days = 31, april : month_days = 30
Use calendar module to get days from months
>>> import datetime
>>> import calendar
>>> now = datetime.datetime.now()
>>> print calendar.monthrange(now.year, now.month)[1]
31
For 2015 Feb month
>>> calendar.monthrange(2015, 2)
(6, 28)
https://docs.python.org/2/library/calendar.html

Categories

Resources