Happy Easter to y'all!
So my problem is essentially using SQL timestamp is causing me some issues. I have a Travel Booking website with a database made in PHPmyadmin and as mentioned two timestamp columns (one for departure time and one for arrival.) If there are times currently there for the journey they will be displayed, if not a tick box to set the current time as the timestamp, this i'm fine with.
I don't know what html form element to use to display the entirety of the SQL timestamp, both the date and time section in the html form (or how to validate any of it xD) I have tried splitting the timestamp and displaying it in both a date and time field but had no luck and was told to stick to the timestamp by my group members. Cheers
Solved the problem with some troubleshooting. The formatting difference between datetime-local and timestamp can be solved with some simple regex.
VariableName = re.sub(r"\s+", 'T', VariableName)
Swaps any whitespace characters with a T.
This is because datetime-local likes to concatenates the date and time together using a capital T. If we simulate this using the regex above we can convert the timestamp into a readable format.
Related
I am using this https://datatables.net/examples/api/multi_filter.html for HTML tables as it provides multiple features.
My Django website passes on dates as strings to the frontend Collection Date in descending order
and also datetime object to display on frontend HTML template.
But it is not sorted correctly.
This website has inbuilt sort facility which is not working fine.
https://datatables.net/examples/api/multi_filter.html
How to sort date string with format "%m-%d-%Y"?
Please help me to resolve this.
Thank you
It would be great if you can post your code as well. But anyways, will recommend one blind fix.
Article
So here you need to mention explicitly on the basis of what you want to sort column. In your case following will be a useful selection.
monthYear
date-de
date-uk
date-euro
date-eu
date-dd-MMM-yyyy
You can choose accordingly.
I have a data which I collected by web-scraping. In my scraping data, there are the different date in the different format, I need to find a particular date from data so I made a regular expression for python that can find a date from any format.
The regular expression I made is:
(?:\w+\/\w+\/\w+|\w+\-\w+\-\w+|\d+\s+\w+\s+\d+)
I got the date in correct syntax but the main task is to retrieve a particular date.I have a string in which i used a regular expression like
The starting date of application is 21/10/2018 and end date is
30/10/2018. The application was issued on 10 November 2018
I want to retrieve start date, issued date and end date. Can Somebody help me..
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)
We are doing an AJAX call in Django, where a user enters a date and a number, and the AJAX call looks up if there already is a document with that number and date.
The application is internationalised and localised. The problem is how to interpret the date sent by AJAX into a valid Python/Django date object. This has to be done using the current users locale, ofcourse.
One solution I found does not work:
Django: how to get format date in views?
get_format() returns a string (in our case j-n-Y), but strftime() expects a format string in the form of %j-%n-%Y.
Why the Django format is different from the stftime() format beats me, FYI we're using Django 1.5 currently.
I think the problem is that in all examples I could find, the dates are already date objects, and Python/Django just does formatting. What we need is to convert the string into a date using the current locale, and THEN format it. I was figuring this would be a standard problem, but all of the possible solutions I found and tried don't seem to work...
Thanks,
Erik
Submitting a ticket to Django gave me a clue to the answer.
You can convert a specific type of data into an object by passing it through the corresponding field and calling to_python(). In my case with a date it would be like so:
from django.forms.fields import DateField
field = DateField()
value = request.GET.get('date', '')
formatted_datetime = field.to_python(value)
Erik
Sounds to me it would make your life easier to let the user select the date from a calendar, instead of typing its string representation.
This way you know exactly what you're getting.
If you know the format, then you should be able to use datetime.strptime .
Have used it in code successfully in conjunction with ajax calls.
See here for more information: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior