How to get day name in datetime in python?

此生再无相见时 提交于 2019-12-17 10:24:18

问题


How can I get the day name (such as: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) in datetime in python?...

Here is my code in my handlers.py

    from django.utils.xmlutils import SimplerXMLGenerator
    from piston.handler import BaseHandler
    from booking.models import *
    from django.db.models import *
    from piston.utils import rc, require_mime, require_extended, validate
    import datetime

    class BookingHandler(BaseHandler):
        allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
        fields = ('id', 'date_select', 'product_name', 'quantity', 'price','totalcost', 'first_name', 'last_name', 'contact', 'product')
        model = Booking

       def read(self, request, id, date_select):
        if not self.has_model():
            return rc.NOT_IMPLEMENTED
        try:   
            prod = Product.objects.get(id=id)
            prod_quantity = prod.quantity
            merge = []
            checkDateExist = Booking.objects.filter(date_select=date_select)
            if checkDateExist.exists():
                entered_date = Booking.objects.values('date_select').distinct('date_select').filter(date_select=date_select)[0]['date_select']
            else:             
                entered_date = datetime.datetime.strptime(date_select, '%Y-%m-%d')
                entered_date = entered_date.date()

            delta = datetime.timedelta(days=3)
            target_date = entered_date - delta
            day = 1
            for x in range(0,7):
                delta = datetime.timedelta(days=x+day)
                new_date = target_date + delta
                maximumProdQuantity = prod.quantity
                quantityReserve = Booking.objects.filter(date_select=new_date, product=prod).aggregate(Sum('quantity'))['quantity__sum']
                if quantityReserve == None:
                    quantityReserve = 0

                quantityAvailable = prod_quantity - quantityReserve
                data1 = {'maximum_guest': maximumProdQuantity, 'available': quantityAvailable, 'date': new_date}
                merge.append(data1)    

            return merge       
        except self.model.DoesNotExist:
            return rc.NOT_HERE

In my code: this line sets the date:

 for x in range(0,7):
                    delta = datetime.timedelta(days=x+day)
                    new_date = target_date + delta

回答1:


import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))

See the Python docs for datetime.now, datetime.strftime and more on strftime.




回答2:


>>> from datetime import datetime as date
>>> date.today().strftime("%A")
'Monday'


来源:https://stackoverflow.com/questions/8380389/how-to-get-day-name-in-datetime-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!