Debug School

rakesh kumar
rakesh kumar

Posted on

How to calculate days ,month ,year from current date to input request datepicker in django

To calculate the number of days, months, and years between the current date and an input request datepicker in Django's views.py, you can use the date module and the timedelta class from the datetime library. Here's an example code that demonstrates five different methods to calculate the duration:

views.py

from django.shortcuts import render
from datetime import datetime, timedelta

def calculate_duration(request):
    current_date = datetime.now().date()

    if request.method == 'POST':
        request_date = datetime.strptime(request.POST.get('datepicker'), '%Y-%m-%d').date()

        # Method 1: Using timedelta and days attribute
        duration_method_1 = (request_date - current_date).days

        # Method 2: Using timedelta and total_seconds() method
        duration_method_2 = (request_date - current_date).total_seconds() / (24 * 60 * 60)

        # Method 3: Using timedelta and components
        duration_timedelta = request_date - current_date
        duration_method_3 = duration_timedelta.days

        # Method 4: Using date.year, date.month, and date.day
        duration_method_4_years = request_date.year - current_date.year
        duration_method_4_months = request_date.month - current_date.month
        duration_method_4_days = request_date.day - current_date.day

        # Method 5: Using divmod() function
        duration_years, remainder = divmod(duration_method_3, 365)
        duration_months, duration_days = divmod(remainder, 30)

        return render(request, 'template.html', {
            'duration_method_1': duration_method_1,
            'duration_method_2': duration_method_2,
            'duration_method_3': duration_method_3,
            'duration_method_4_years': duration_method_4_years,
            'duration_method_4_months': duration_method_4_months,
            'duration_method_4_days': duration_method_4_days,
            'duration_method_5_years': duration_years,
            'duration_method_5_months': duration_months,
            'duration_method_5_days': duration_days,
        })

    return render(request, 'template.html')
Enter fullscreen mode Exit fullscreen mode

template.html

<html>
<head>
    <title>Duration Calculation Example</title>
</head>
<body>
    <form method="POST">
        {% csrf_token %}
        <label for="datepicker">Select Date:</label>
        <input type="date" id="datepicker" name="datepicker">
        <button type="submit">Calculate Duration</button>
    </form>

    <h2>Method 1: Using timedelta and days attribute</h2>
    <p>Duration: {{ duration_method_1 }} days</p>

    <h2>Method 2: Using timedelta and total_seconds() method</h2>
    <p>Duration: {{ duration_method_2 }} days</p>

    <h2>Method 3: Using timedelta and components</h2>
    <p>Duration: {{ duration_method_3 }} days</p>

    <h2>Method 4: Using date.year, date.month, and date.day</h2>
    <p>Duration: {{ duration_method_4_years }} years, {{ duration_method_4_months }} months, {{ duration_method_4_days }} days</p>

    <h2>Method 5: Using divmod() function</h2>
    <p>Duration: {{ duration_method_5_years }} years, {{ duration_method_5_months }} months, {{ duration_method_5_days }} days</p>
</body
Enter fullscreen mode Exit fullscreen mode

Another Example


from django.shortcuts import render
from django.db.models import Sum, Case, When
from django.db.models.functions import ExtractYear, ExtractMonth, ExtractDay, ExtractTime, Now
from datetime import datetime
from .models import Student, Subject

def retrieve_all_data(request):
    current_date = datetime.now()

    # Method 1: Using filter() and annotate() methods with whereDate
    students_method_1 = Student.objects.filter(subject__date__date__date=request_date).annotate(total_score=Sum('subject__score')).order_by('name')

    # Method 2: Using filter() and annotate() methods with whereMonth
    students_method_2 = Student.objects.filter(subject__date__date__month=request_date.month).annotate(total_score=Sum('subject__score')).order_by('name')

    # Method 3: Using filter() and annotate() methods with whereDay
    students_method_3 = Student.objects.filter(subject__date__date__day=request_date.day).annotate(total_score=Sum('subject__score')).order_by('name')

    # Method 4: Using filter() and annotate() methods with whereYear
    students_method_4 = Student.objects.filter(subject__date__date__year=request_date.year).annotate(total_score=Sum('subject__score')).order_by('name')

    # Method 5: Using filter() and annotate() methods with whereTime
    students_method_5 = Student.objects.filter(subject__date__time__hour=request_date.hour).annotate(total_score=Sum('subject__score')).order_by('name')

    return render(request, 'template.html', {
        'students_method_1': students_method_1,
        'students_method_2': students_method_2,
        'students_method_3': students_method_3,
        'students_method_4': students_method_4,
        'students_method_5': students_method_5,
    })
Enter fullscreen mode Exit fullscreen mode

template.html

<html>
<head>
    <title>Retrieving All Data Example</title>
</head>
<body>
    <!-- Method 1: Using filter() and annotate() methods with whereDate -->
    <h2>Method 1: Using filter() and annotate() methods with whereDate</h2>
    {% for student in students_method_1 %}
        <p>Name: {{ student.name }}</p>
        <p>Total Score: {{ student.total_score }}</p>
        <hr>
    {% empty %}
        <p>No students found</p>
    {% endfor %}

    <!-- Method 2: Using filter() and annotate() methods with whereMonth -->
    <h2>Method 2: Using filter() and annotate() methods with whereMonth</h2>
    {% for student in students_method_2 %}
        <p>Name: {{ student.name }}</p>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)