'How to filter a query according to creation date?

I have a query and I want to list the objects that created today.

query_emails = Email.objects.values('mail_from').annotate(mail_count=Count('mail_from'), mailts=F('mail_timestamp')).order_by()

mail_timestamp is the created day and it should be today, so like

.filter(mail_timestamp=today)

The output of the mail_timestamp is

'mailts': datetime.datetime(2021, 11, 8, 8, 9, 35, tzinfo=<UTC>)

I used today = datetime.now() but it didn't work.

How can I filter this type date?



Solution 1:[1]

from datetime import date
date.today()

this should work

Solution 2:[2]

today means that mail date have to be lower than tomorrow's 00:00:00 and bigger than yesterday's 23:59:59:

from datetime import datetime, timedelta
import pytz

from_date = (datetime.now() - timedelta(1)).replace(hour=23, minute=59, second=59, microsecond=999999, tzinfo=pytz.UTC)
to_date = (datetime.now() + timedelta(1)).replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC)

# if mail_timestamp field type is Datetime:
Email.objects.filter(mail_timestamp__lt=to_date).filter(mail_timestamp__gt=from_date)

# if mail_timestamp field type is timestamp (integer representation):
Email.objects.filter(mail_timestamp__lt=datetime.timestamp(to_date)).filter(mail_timestamp__gt=datetime.timestamp(from_date))

Solution 3:[3]

First you need to calculate the ratio of the change for the currency which had its value modified, you'll then just map the values of the dictionary with that ratio:

extension Dictionary where Key == String, Value == Double {
     mutating func applyChange(of currency: Key, with newValue: Value) {
             guard
                newValue != .zero,
                let oldValue = self[currency],
                oldValue != newValue,
                oldValue != .zero
        else { return }
             
        let ratio = 1.0 + ((newValue - oldValue) / oldValue)
        indices.forEach { values[$0] *= ratio }
    }
}

Solution 4:[4]

If I've understood you correctly, I believe this would work.

Each time the user adjusts the value in the textField you'd loop through your rates dictionary, calculate the new rate and return an updated dictionary.

If I've misunderstood your goal please let me know and I'll do my best to assist :)

struct Converter {
    
    let exchangeRates: [String: Double] = [
        "USD": 1,
        "AED": 3.67,
        "AFN": 105,
        "ALL": 107.39,
        "AMD": 481.52,
        "ANG": 1.79,
        "AOA": 538.31
    ]
    
    func convert(_ amount: String) -> [String: Double] {
        
        var newRates = [String: Double]()
        
        exchangeRates.forEach { (code, value) in
            if let amount = Double(amount) {
                newRates[code] = value * amount
            }
        }
        
        return newRates
    }
    
}

let converter = Converter()
let userInput = "10.00"

print(converter.convert(userInput))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hemal
Solution 2
Solution 3 valeCocoa
Solution 4 j.rich