In my Azure Function, I configured environment variable WEBSITE_TIME_ZONE as Europe/London, hosted this on Azure Linux App Service Plan with “North Europe Zone” and in my base class, I created Date Objects as below:
public class BaseService
{
public DateTime Yesterday { get; set; }
public DateTime Today { get; set; }
public BaseService()
{
SeedData();
}
private void SeedData()
{
// Configure Processing Dates
bool isManul = Environment.GetEnvironmentVariable(ConfigConstants.IsManual).ExactBool();
DateTime processingDate = isManul ? Environment.GetEnvironmentVariable(ConfigConstants.ManualDate).ExactDateTime() : DateTime.Now;
Yesterday = processingDate.AddDays(-1);
Today = processingDate;
}
}
In another service that is inherited by this base class, I’m using these date objects to query MongoDB
public class MyService : BaseService
{
var filter = fd.Gte(FieldConstants.CreatedDate, Yesterday.Date)
& fd.Lt(FieldConstants.CreatedDate, Today.Date);
////
}
Here, I observed that in MongoDB log (Assuming Today is “27-jun-2024”) the Query utilized wrong Date with TimeZone issue:
Expected
"query": {
"processedDate": {
"$lt": {
"$date": "2024-06-26T00:00:00Z"
}
}
},
Actual
"query": {
"processedDate": {
"$lt": {
"$date": "2024-06-25T23:00:00Z"
}
}
},