Get the day of week given a date - python-3.x

I am working on my project on a timetable app. I can get the date with:
import datetime
now = datetime.datetime.now()
print (now.day, "/", now.month, "/", now.year)
But I can't get the day of the week though. Can someone help me?

To format and print dates, you should use the strftime functions (see the strftime python 3 documentation) instead of manually build your own format.
so e.g.
import datetime
now = datetime.datetime.now()
print(now.strftime("%A, %d/%m/%Y"))
Check out the doc, for the full list of styles. Maybe you want %a (abbreviated weekday name, or also %b or %B for the month name.
If you need just the values, check the datetime documenation, in the same page: you have now.weekday() (Monday is 0 and Sunday is 6), or now.iweekday() (Monday is 1 and Sunday is 7).

Try this:
import time
localtime = time.localtime(time.time())
current_time = time.asctime(localtime)
print(current_time[:3])
This should work.
Thanks.

There is an excellent answer by #seddonym on https://stackoverflow.com/a/29519293/4672536 to find the day of the week. I will post the code here for reference. Good luck! :) :
>>> from datetime import date
>>> import calendar
>>> my_date = date.today()
>>> calendar.day_name[my_date.weekday()]
'Wednesday'

Related

Python Time zone: get abbreviation and UTC offset

my time zone is "Australia/Melbourne" (I have multiple zones like this when I give this to my function) and I need the output like this ASET(GMT +10). How can I reach my answer?
Thank you
assuming you have date and time available (see my comment), the easiest way is probably strftime:
from datetime import datetime
from dateutil import tz
timezone = tz.gettz("Australia/Melbourne")
dt = datetime.now(timezone)
print(f"{dt.strftime('%Z')}(GMT{dt.strftime('%z')})")
# AEST(GMT+1000)
If you exactly want to get the specified output, I suppose you have to go a little more sophisticated:
total_minutes, seconds = divmod(dt.utcoffset().total_seconds(), 60)
hours, minutes = divmod(total_minutes, 60)
utcoff = f"{int(hours):+d}:{int(minutes):02d}" if minutes else f"{int(hours):+d}"
print(f"{dt.strftime('%Z')}(GMT{utcoff})")
# AEST(GMT+10)

Today's date without time aspect

I am currently trying to achieve an output which gives today's date without time.
I currently have the code
import datetime as date
today = date.datetime.today().strftime("%Y-%m-5%d")
You can simply do this:
from time import strftime
print(strftime('%b %d, %Y')) #or use '%Y-%m-%d'

Time data from API convert to Date time python

i have this problem with milliseconds or microseconds data from api im not totally sure. I am trying to convert this to a readable date time. below is an example. The web app has a dashboard which you i can check the date time. but i do not know exactly how to convert it to a readable date time.
Example 1:
FROM API
"start":1542243678,
FROM Dashboard
11/15/2018 9:01 am
Example 2:
FROM API
"end":1542330078,
FROM Dashboard
11/16/2018 9:01 am
When i try to convert to python date time it gives me wrong info.
import datetime
import time
milliseconds = 1542243678
date = datetime.datetime.fromtimestamp(milliseconds/1000.0)
date = date.strftime('%Y-%m-%d %H:%M:%S')
print(date)
Output:
1970-01-19 04:24:03
Sorry if I don't understand your question correctly, but is this what you want?
import datetime
import time
milliseconds = 1542243678
date = datetime.datetime.fromtimestamp(milliseconds)
date = date.strftime('%m/%d/%Y %I:%M %p')
print(date)
Output: 11/14/2018 05:01 PM
You don't need to divide by 1000 before passing to datetime.datetime.fromtimestamp.
Try datetime.datetime.fromtimestamp(1542243678). It should work.

Spell out each word of a date in Python 3

In Python 3, I would like to change 2018-01-01 to "January first, two thousand and eighteen". I looked at the format guide in the Python datetime documentation
but I did not see a format for spelling out the day of the month or for spelling out the year. Is there a well-known module that can do this (I am new to Python)?
Using the inflect module, as commented by # Lakshay Garg you can do:
import datetime as dt, inflect
p = inflect.engine()
def date_in_words(x):
a = dt.datetime.strptime(x,'%Y-%m-%d')
return (a.strftime('%B ')+p.number_to_words(p.ordinal(int(a.day)))+', ' +
p.number_to_words(int(a.year)))
date_in_words('2018-03-14')
Out[259]: 'March fourteenth, two thousand and eighteen'

Python string to datetime-date

I've got lots of dates that look like this: 16.8.18 (American: 8/16/18) of type string. Now, I need to check if a date is in the past or future but, datetime doesn't support the German format.
How can I accomplish this?
from datetime import datetime
s = "16.8.18"
d = datetime.strptime(s, "%d.%m.%y")
if d > datetime.now():
print('Date is in the future.')
else:
print('Date is in the past.')
Prints (today is 20.7.2018):
Date is in the future.
The format used in strptime() is explained in the manual pages.

Resources