5

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' 
2
  • 2
    Not to answer your question directly, but why wouldn't you just use DateField? Also instead of modified = models.DateTimeField(default=datetime.datetime.now) you can use auto_now=True, that will update the field every time it's changed. datetime.datetime.now is a TZ-unaware timestamp, which might cause issues later on. Commented Apr 24, 2014 at 7:09
  • 1
    obj.modified.date().isoformat() Commented Apr 24, 2014 at 7:17

5 Answers 5

3

this works for me.

def get_date(self): return self.modified.date() 
Sign up to request clarification or add additional context in comments.

1 Comment

or can be done from a template with {{inventory_item.modified.date}}
1

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") 

2 Comments

this one stays or i remove this ` modified = models.DateTimeField(default=datetime.datetime.now)`
I don't understand your comment. Sry
0

This is code will solve your problem:

def get_date(dt): """ Remove the time from datetime field. """ return dt.strftime("%Y-%M-%d") @property def modified_date(self): return self.get_date(self.modified) 

You can also use DateField instead of DateTimeField

If you want in django templatetag date formatting click here.

Comments

0

Just replace modified = models.DateTimeField(default=datetime.datetime.now) with modified = models.DateField(default=datetime.date.now)

Comments

0

You can store the time if required, else you can use the Date Field as mentioned here.

However after using DateTimeField in your models, to fetch only date via Django's ORM you can simply do the following:

To Get the Date Only:

User.objects.filter('created_at__date') 

To Get the Month Only:

User.objects.filter('created_at__month') 

To Get the Year Only:

User.objects.filter('created_at__year') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.