I have been facing issue working with leave request. The problem is after I create a leave request and try to fetch the duration of the date_from field in my module field.date 'get_date', I get date as 1 day before . Like if in leave request the 'date_from'=17/11/2015 , it shows on my field as 'get_date'=16/11/2015. The 'date_from' field is saving as 1 day before in database as well. I am not sure is this a timezone issue or I have to use some other method to fetch date to avoid the problem.
Related
I'm working on a website that has to do with user entering data to fields of a table. There is (input type=date) in top of the page. I want the user to enter values daily. but I want to save that data in the database according to that day and if the user change the date, I want to clear some fields for that day for a new fresh entry.
I also don't know if there should be a datefield in model or not?? If so, would I have to populate the database with lots of dates? and if it reach the last date, I have to populate it again and so on??
How I go about doing this?
I tried looking for a solution but couldn't find one that seems good. I found I could use crontab in django or scheduler but I don't think this is the optimum solution.
I'm pretty new to python, I have been able to connect to an API and get the data I want. However I notice that the date field has be hardcoded in the URL.
https://api.eia.gov/v2/electricity/rto/fuel-type-data/data?api_key=XXXX&start=2022-05-18T00
I plan to run the script daily and I am only interested in the past week of data. Is there a way to code this so I don't need to change the date manually everyday?
Thanks!
In my form I have a field that I want to default as Today when launching the form:
class ExpenseForm(forms.ModelForm):
date = forms.DateField(initial=datetime.now(), widget=forms.DateInput(attrs={'id':'datepicker'}))
Now if I go to the form I see it working correctly, it's got today's date populated, (for example "2018-12-03") but then when I come in tomorrow, it STILL has yesterday's date populated. In fact it will not update until I somehow restart the server.
Any idea?
Thank you.
I am working on Python/Django project. I am trying to let user select date and time using jQuery plugin datetimepicker add-on. So when I select now option and post data django is saving the time in UTC offset. This is what is saved in database, 2017-03-30 13:38:00+00:00. I need to convert this time from user's timezone and save it in system as utc. Because later I will have script running which will look for data in database which is less than the utc time.
Actually the script is to let user post information on website and let them chose the publish date and time. So for example, If use posted an article which will be published on April 2nd 1pm Chicago time, I don't want other users to read the article before this time. So people around the world can read article as soon as it is April 2nd and 1PM in Chicago. So how can I make this functionality work?
My solution was to get the time and remove it's timezone information using replace(tzinfo=pytz.timezone('America/Chicago')) and when I print the time, I am getting 2017-03-30 13:38:00-05:51. The actual offset right now is -05:00. Can anyone help me to and tell me what I am doing wrong?
What I am doing for form is that I have publish_date object in my model and I am using django forms to create form. I have added class as an attribute in it and using jquery plugin,
$('.datepicker').datetimepicker({
timeFormat: 'HH:mm',
stepHour: 1,
stepMinute: 1,
});
So when User submits the form, on post method this my code,
form = PublishForm(request.POST)
if form.is_valid():
f = form.save(commit=False)
f.created_by_user_id = request.user.id
f.save()
and than to get the date all I am doing is f.publish_date and the other options I have used lice replace and localize are pretty standard.
Thanks
As noted in the comments, you appear to have two problems. First is that the datetimepicker is saving a date and time with a UTC timezone offset, when it should be applying a different timezone or leaving off the timezone offset entirely. Second is that pytz is using the wrong offset.
I don't know how to fix the first problem, but I can help you with the second. You need to use pytz's localize function. This only works when the datetime doesn't have a timezone attached, but since you know the timezone is incorrect anyway you can delete it first.
tz = pytz.timezone('America/Chicago')
dt = tz.localize(dt.replace(tzinfo=None))
The naming of the datetime replace(tzinfo = ...) function is unfortunate. In fact, its behaviour is random. Do not use this!
Mark's answer is the way to go. Use localize.
Currently I have a script that queries TS3 servers and then stores their SID and amount of players in a database table that can be used by Django to render charts. Everything works, including the rendering of the charts however I am unable to remove the seconds from a date when saving. I have a variable called "startTime" to get the date and time. This works and gives an output like this "04-04-2013 16:58" however when saving it to the database, I get something like this "04-04-2013 16:58:32". This is what I am using to get the date and time.
startTime = datetime.now().strftime("%d-%m-%Y %H:%M")
Any ideas on how to remove the seconds before saving or even after by updating the rows in the database? I am using a Charfield to store the date.
Use the datetime.replace method like this: datetime.now().replace(second=0, microsecond=0)