编程语言
首页 > 编程语言> > 如何在python中获取datetime中的日期名称?

如何在python中获取datetime中的日期名称?

作者:互联网

如何在python的datetime中获取日期名称(例如:星期一,星期二,星期三,星期四,星期五,星期六和星期日)?…

这是我的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

在我的代码中:此行设置日期:

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

解决方法:

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

请参阅datetime.now,datetime.strftimemore on strftime的Python文档.

标签:python,datetime,django,django-piston
来源: https://codeday.me/bug/20190925/1817519.html