Today's date without time aspect - python-3.x

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'

Related

Using timezone with openpyxl

I need to add a date in an excel file.
I use timezones on these dates. It works very well with django rest framework. (My GET requests return the date with this format: 2022-07-23T13:19:59+02:00) Same in Django admin, the Europe/Paris timezone is well taken into account (+02:00).
However, when I use openpyxl, the indicated time is 2h late (it takes the UTC timezone).
sheet.append([date_recorded.strftime("%d/%m/%Y %H:%M:%S")])
Same thing if I print this date in console. (2022-07-11 15:19:50+00:00)
How to correct this?
You can simply convert it to your desired zone like this:
from dateutil import tz
utctime = "YOUR CURRENT VALUE OF DATETIME"
from_zone = tz.gettz("UTC")
to_zone = tz.gettz("yourzone")
utctime = utctime.replace(tzinfo=from_zone)
new_time = utctime.astimezone(to_zone)
Since you are using django, you can use built-in get_current_timezone to convert datetime objects to the local timezone:
from django.utils import get_current_timezone
timezone = get_current_timezone()
date_recorded = date_recorded.astimezone(timezone)
sheet.append([date_recorded.strftime("%d/%m/%Y %H:%M:%S")])

Change how datetime object is printed from print()?

When printing dates in the datetime module of Python 3.6, they look like:
>> from datetime.date import date
>> print(date(2018,1,30))
2018-01-30
Is it possible to change the format of the output of print() for datetime.date objects?
My desired outcome is:
>> print(date(2018,1,30))
30/01/18
You can use datetime formatting function - strftime - taking a formatting string described in detail here.
You may also want to review Advanced date formatting from the following answer: How to print a date in a regular format?

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.

Get the day of week given a date

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'

Setting Loop with start and end date

Thats a code a friend of mine helped me with in order to get files from diferent measurement systems, timestamps and layout into on .csv file.
You enter the timeperiod or like in the case below 1 day and the code looks for this timestamps in different files and folders, adjusts timestamps (different Timezone etc.) and puts everything into one .csv file easy to plot. Now I need to rewrite that stuff for different layouts. I managed to get everything working but now I don't want to enter every single day manually into the code :-( , cause I'd need to enter it 3 times in a row --> in order to get the day for one day into one file, dateFrom and dateTo needs to be the same and in the writecsv...section you'd have to enter the date again.
here's the code:
from importer import cloudIndices, weatherall, writecsv,averagesolar
from datetime import datetime
from datetime import timedelta
dateFrom = datetime.strptime("2010-06-21", '%Y-%m-%d')
dateTo = datetime.strptime("2010-06-21", '%Y-%m-%d')
....
code
code
....
writecsv.writefile("data_20100621", header, ciData)
what can I change here so that I get an automatic loop for all data between e.g 2010-06-21 to 2011-06-21
p.s. if i'd entered 2010-06-21in dataFromand 2011-06-21 in dateTo i'd get a huge cvs. file with all the data in it ..... I thought that would be a great idea but it's not really good for plotting so I enden up manually entering day after day which isn't bad if you do it on a regular basis for 2 or 3 days but now a dates showed up and I need to rund the code over it :-(
Generally speaking you should be using datetime.datetime and datetime.timedelta, here is an example of how:
from datetime import datetime
from datetime import timedelta
# advance 5 days at a time
delta = timedelta(days=5)
start = datetime(year=1970, month=1, day=1)
end = datetime(year=1970, month=2, day=13)
print("Starting from: %s" % str(start))
while start < end:
print("advanced to: %s" % str(start))
start += delta
print("Finished at: %s" % str(start))
This little snippet creates a start and end time and a delta to advance using the tools python provides. You can modify it to fit your needs or apply it in your logic.

Resources