I need the date in a regular form.
Here when I use the below code:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
date = QDate.currentDate()
print date
I get the below output:
PyQt4.QtCore.QDate(2014, 5, 23)
I want the date to be displayed in '/' or '.' eg.(5.23.2014)
Please suggest how to do it.
Check the docs:
date = QDate.currentDate().toString("dd.MM.yyyy")
print date
2014.5.23
To change:
date = QDate.currentDate().toString("MM.dd.yyyy")
print date
05.23.2014
Related
I am trying to create a new datetime column from existing columns (one datetime column and another integer column) in a pandas data frame. Here is my code:
import datetime
df['start_date'] = pd.to_datetime(df['start_date'])
df['end_date'] = df['start_date'] + pd.Timedelta(df.total_waiting_days, unit='D')
But I got the following errors:
ValueError: Value must be Timedelta, string, integer, float, timedelta or convertible
What did I do wrong here and how do I fix this? Thanks!
Seems like you want to convert whole column to Timedelta
df['end_date'] = df['start_date'] + df.total_waiting_days.apply(lambda x :pd.Timedelta(x, unit='D'))
How do I convert a string to date time in Python ? I have the string 09082010 .how do I convert to 09-08-2010?.Thank you.
from datetime import datetime
mydate = datetime.strptime('09082010', '%d%m%Y')
If all of your date strings will be in the format mmddyyyy like the example you provided then this should work for you.
import datetime
d="09012010"
mm=d[:2]
dd=d[2:4]
yr=d[4:]
date = datetime.date(day=dd, month=mm, year=yr)
In excel contains details of so many weeks,I need to print only current week number from excel sheet,if only current week conditions matches it should print that
piece of code for that condition but it showing error:
rec_date = datetime.datetime(*xlrd.xldate_as_tuple(rec, inputfp.datemode)).isocalendar()[1]
if rec_date == date.today().isocalendar()[1]:
print '\n',rec_date
print str(out[rec])
Showing error:
if rec_date == date.today():
AttributeError: 'float' object has no attribute 'today'
So I presume you do not import date from datetime but just import datetime. So
if rec_date == datetime.date.today()
shall work. Or from datetime import date
I'm trying to make a progress bar reflecting pandas dataframe build progress from sql. Currently I have a table with 9 columns containing 1000 records.
import pandas as pd
import psycopg2 as ps
import pandas.io.sql as psql
conn = ps.connect(user="user", password="password", database="database")
sql = "select * from table"
a = datetime.datetime.now()
df = psql.read_frame(sql, con=conn)
---and blablabla some little functions
b = datetime.datetime.now()
print b-a
instead of getting delta start & end time of the function, I would prefer and it would be nice to show progress bar to end user (just in case the data is getting bigger), so they have idea how long it would take. Is that possible? how to do it?
Does anyone know how I can sort (in this instance date) my django query set against todays date ?
class Person(models.Model):
name = models.CharField(max_length=50)
date = models.DateField()
My goal is to list the name and date entries. At the top of the list will be the entry with the date that is closest to todays date (day/month).
You can use extra queryset method to select additional data from database table.
This is example that works with MySql:
Person.objects.extra(select={
'datediff': 'ABS(DATEDIFF(date, NOW()))'}).order_by('datediff')
DATEDIFF - returns difference in days bewteen two dates,
ABS - returns absolute value. For sqlite, there is different syntax, see this answer.
EDIT: use current year
Person.objects.extra(select={
'datediff': "ABS(DATEDIFF(CONCAT(YEAR(now()), '-', MONTH(date), '-', DAY(date)), NOW()))"}
).order_by('datediff')
EDIT 2: optimized *
from datetime import date
dayofyear = int(date.today().strftime("%j"))
datediff = 'LEAST(ABS(DAYOFYEAR(date) - %d), ABS((366 - %d + DAYOFYEAR(date))) MOD 366)' % (
dayofyear, dayofyear
)
Person.objects.extra(select={'datediff': datediff}).order_by('datediff')
EDIT 3: closest date after given (todays) date
from datetime import date
dayofyear = int(date.today().strftime("%j"))
datediff = '(DAYOFYEAR(date) - %d + 365) MOD 365' % (
dayofyear
)
Persion.objects.extra(select={'datediff': datediff}).order_by('datediff')
If you want to sort based on date, you can order as: .order_by('date') on a result queryset.
I'm not sure if that answers your question. In case you mean you want to select only the Persons with date of today, you can use:
import datetime
now = datetime.datetime.now()
persons_with_date_today = Person.objects.filter(date=now)