Get the date name from a datetime object [duplicate] - python

This question already has answers here:
How to get day name from datetime
(6 answers)
Closed 3 years ago.
Is it possible to get the name of a day (e.g. Monday) from a datetime object?
In my pandas dataframe I have a column of type datetime e.g.
2016-07-01
and so on
I want to create a new column with the name of that day
Thanks in advance

duplicate of this?
How to get day name in datetime in python?
import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))
or:
>>> from datetime import datetime as date
>>> date.today().strftime("%A")
'Monday'

Related

How do I remove hours and seconds from my DataFrame column in python? [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 10 months ago.
I have a DataFrame :
Age Gender Address Date
15 M 172 ST 2022-02-07 00:00:00
I Want to remove hh:mm:ss
I tried:
import datetime as dt
df["Date"]=df["Date"].dt.Date .
But I am receiving no change in date column format.
All I want is that the date column has only (YYYY-MM-DD).
You can use pd.to_datetime to convert Date column to datetime object.
df['Date'] = pd.to_datetime(df['Date']).dt.date
# or
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
# or
df['Date'] = df['Date'].str.split(' ').str[0]

How to format datetime object to date [duplicate]

This question already has answers here:
How do I convert a datetime to date?
(10 answers)
Closed 3 years ago.
I have a datetime object
v = 21.01.2019 14:25:37
I want to convert above datetime object to date as this
a = convert(v)
a = 21.01.2019
how i can do it
If you want today's date.try this :
datetime.datetime.now().date()
If you want your datetime object date :
v.date()

How to convert date as string to date type [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have a variable '2019-05-30 21:01:09' that needs to be converted into 2019-05-30 21:01:09. What is the best way to go about this.
from datetime import datetime
strToDate = datetime.strptime('2019-05-30 21:01:09','%Y-%m-%d %H:%M:%S')
strptime() allows for the conversion of string to date time object provided that you give the format the function should expect as the second argument
You can using datetime library
from datetime import datetime
datetime.strptime('2019-05-30 21:01:09', '%Y-%m-%d %H:%M:%S')
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Comparing date in python and applying a condition on comparison [duplicate]

This question already has answers here:
How to compare two dates?
(6 answers)
Closed 3 years ago.
i want to compare/subtract two date's due date from current date in python and apply a condition on this subtraction that if the difference is >0 days then calculate fine by multiplying difference with fine per day
from datetime import date
a=date.today()
issuedate=date(2019,5,9)
duedate#should be 5 days after issue date i can't find the method for doing this
check=a-duedate
# if check>0days:
# print(check days*40)
You need to use the timedelta function in the datetime module:
from datetime import date
import datetime
a=date.today()
issuedate=date(2019,5,9)
duedate = issuedate+datetime.timedelta(days=5)
check=a-duedate
print(check>=datetime.timedelta(days=0))
Use timedelta
from datetime import timedelta
duedate = issuedate + timedelta(days=5)
check=(a-duedate).days

Converting UTC-Date-String to Unixtimestamp [duplicate]

This question already has answers here:
Parsing datetime strings containing nanoseconds
(5 answers)
Closed 4 years ago.
I have a date formated like this: "2018-06-12T13:58:36.663550655Z"
I want be convert this string into a Unix time stamp.
This is my code:
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
time = datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ")
unixTime = datetime.utcfromtimestamp(time)
When i run it, there's a error:
ValueError: time data '2018-06-12T14:03:35.306662173Z' does not match
format '%Y-%m-%dT%H:%M:%S.%fZ'
Would be nice if someone could help me !
Thanks!
Try stripping the extra info.
Ex:
import time
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
d = datetime.strptime(date[:26],"%Y-%m-%dT%H:%M:%S.%f")
unixTime = time.mktime(d.timetuple())
print(unixTime)
Output:
1528792415.0

Categories