Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove time from django DateTime in Django

Tags:

python

django

So i need to remove from date Time, so i only get date, year and month.

"2014-04-17T00:00:00"

I looked at different opportunities but it didn't work

class Inventory(models.Model):
manufacturer = models.CharField(max_length=255)
model        = models.CharField(max_length=255)
description  = models.TextField(max_length=255)
count        = models.IntegerField(max_length= 255, default=1)
location     = models.ForeignKey('Location',  null=True, blank=True)
cover        = models.FileField(upload_to = 'static/images/', default = 'static/images/no-image.png')
created      = models.DateTimeField(auto_now_add=True)
barcode      = models.CharField(max_length=255)
assigned     = models.ForeignKey(User, blank=True, null=True)
checked      = models.BooleanField(default=False)
modified     = models.DateTimeField(default=datetime.datetime.now)
tags         = TaggableManager(through=None, blank=True)

def __unicode__(self):
    return '%s' % date(self.modified, "n/j/Y")

def format_date(obj):
    return obj.modified.strftime('%d %b %Y %H:%M')
    format_date.short_description = 'Modified'
like image 518
Erick Avatar asked Oct 01 '22 23:10

Erick


2 Answers

this works for me.

def get_date(self):
    return self.modified.date()
like image 65
Rizwan Mumtaz Avatar answered Oct 03 '22 11:10

Rizwan Mumtaz


You can parse the strftime with dateutil.parser :

import dateutil.parser
datetime_date = dateutil.parser.parse(strf_date)

And then get a strftime date with the part of the datetime you need. In your case, Year, Month, Day :

date_only = datetime_date.strftime("%Y-%M-%d")
like image 21
Laurent Avatar answered Oct 03 '22 13:10

Laurent