Is it possible to do a QuerySet filter that returns object form a specified, recurring time span?
For example, given a start and an end date a few days apart, I would like to be able to find:
Get all objects with timestamp between 2pm and 7pm for every day between start and end
Get all objects with timestamp between 1am and 8am for every day that is either a saturday or a sunday.
NOT TESTED
For the first case, assuming MyModel and your timestamp is called date you can do:
import datetime
start_date = datetime.date(2012,7,1)
end_date = datetime.date(2012,7,16)
start_time = datetime.time(14,0)
end_time = datetime.time(19,0)
date_range = end_date - start_date
range_list = None
for days in xrange(date_range.days):
d = start_date + datetime.timedelta(days)
if range_list is None:
range_list = Q(date__range = (datetime.datetime.combine(d, start_time), datetime.datetime.combine(d, end_time)))
else:
range_list = range_list|Q(date__range = (datetime.datetime.combine(d, start_time), datetime.datetime.combine(d, end_time)))
MyModel.objects.filter(range_list)
Related
Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
My objective is to count Lead records based on simple date range query in local time
Integer TotalLeads = [ Select Count() From Lead where createddate >= fromdate CreatedDate <= todate];
Fairly basic. However, the issue I'm running into is I only want to count the leads for the "local" time not UTC; createddate is in UTC for lead records.
Sample dates:
From: 03/23/2017
To: 03/29/2017
For these sample dates and my local time is UTC - 7 (Los Angeles), so my query would be
Integer TotalLeads = [ Select Count() From Lead where createddate >= 2017-03-23T07:00:00z AND CreatedDate <= 2017-03-30T06:59:59z];
If these are my dates, how do I append the local time so from date is 2017-03-23T07:00:00z and to date is 2017-03-30T06:59:59z?
Using from date first, I was able to do the following but can't figure how to keep it in local time
// Date
date ds = date.valueof('2017-03-23');
string dm = string.valueOf( ds.month());
string dd = string.valueOf(ds.day());
string dy = string.valueOf(ds.year());
// DateTime Midnight (UTC)
String SDate = string.valueof(dm +'/' + dd + '/' + dy + ' 12:00 AM');
system.debug(SDate); // -> 3/23/2017 12:00 AM
// DateTime (Local Time)
datetime ds2 = datetime.parse(SDate );
system.debug(ds2); // -> 2017-03-23 07:00:00
system.debug(ds2.format('yyyy-MM-dd\'T\'hh:mm:ss'')); // -> 2017-03-23T12:00:00
As you can see, using ds2.format put its in the format I need but back to UTC (midnight), I need it to be 2017-03-23T07:00:00
Any suggestions?
Thanks!
Figured what I was doing wrong. The date calculation was fine, it had to do with how this was being passed to a batch job.
I've been given an excel document in which worktime information is noted, this document contains certain columns which are being read by using SSIS in visual studio, after that the information is writen to a Database.
The week and year column contain the week number and the year, the columns Monday up to Friday contain information about how many working hours have been spent on a certain task on that day of the week.
What I'd like to do is take the WeekNr, Year and Day and convert these into a date. I've been trying to accomplish this by using a script component that converts a day number, week number and year to a date but so far I haven't been able to get the day number from the columns. In my opinion it would work best if used with a start and end date taking the first and last date of that week.
So my question is if someone knows how to accomplish this, or if I should try a different approach.
The script component:
public override void Input0_ProcessInputRow(Input0Buffer Row, CultureInfo cultureInfo, int day )
{
DateTime firstDayOfYear = new DateTime(Int32.Parse(Row.Jaar), 1, 1);
int firstWeek = cultureInfo.Calendar.GetWeekOfYear(firstDayOfYear, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
int dayOffSet = day - (int)cultureInfo.DateTimeFormat.FirstDayOfWeek + 1;
Row.TaskDates = firstDayOfYear.AddDays((Int32.Parse(Row.Week) - (firstWeek + 1)) * 7 + dayOffSet + 1);
}
Based on this answer, I think you want something like the following. This result gives you Monday's date, so you can just AddDays based on the column day of the week.
DateTime jan1 = new DateTime(Int32.Parse(Row.Jaar), 1, 1);
int daysOffset = DayOfWeek.Monday - jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstMonday, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
var weekNum = Int32.Parse(Row.Week);
if (firstWeek <= 1)
{
weekNum -= 1;
}
var mondaysDate = firstMonday.AddDays(weekNum * 7);
var tuesdaysDate = mondaysDate.AddDays(1);
I'm trying to filter the query set in the BaseDatatableView on a date that's entered in by the user in the following format: mm/dd/yyyy. So start_date is in that format and will get converted to a datetime with strptime, see below.
I'd like to compare it to exactly a date datetimefield in the db, but i'd like to match the month, day, year exactly, disregarding the time. This is what I have so that doesn't work.
class AppointmentListJson(LoginRequiredMixin, BaseDatatableView):
....
start_date = params.get('start_date', '')
if start_date:
qs = qs.filter(start_date__contains=datetime.strptime(
start_date, DATE_FORMAT))
return qs
Thanks
Best Option (Django 1.9+ . - For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.)
# Assumes the start_date variable is a datetime.date() instance or
# a value such as '2019-07-11'.
qs.filter(start_date__date=start_date)
# Note that you can also "chain" this result
qs.filter(start_date__date__lte=start_date)
Options for Django < 1.9
One option (break the date down in the filter):
start_date = datetime.strptime(start_date, DATE_FORMAT)
qs = qs.filter(
start_date__year=start_date.year,
start_date__month=start_date.month,
start_date__day=start_date.day
)
Another option (set the min/max time for the date and use range):
from datetime import datetime
start_date = datetime.strptime(start_date, DATE_FORMAT)
start_date_range = (
# The start_date with the minimum possible time
datetime.combine(start_date, datetime.min.time()),
# The start_date with the maximum possible time
datetime.combine(start_date, datetime.max.time())
)
qs = qs.filter(start_date__range=start_date_range)
i have an object which has 2 dates startdate_c and enddate_c .
i need to find a way to find the days of week these dates fall in
For example
startdate = 1 jun 2012 and enddate = 3 jun2012
I need to know which days of the week the days between these dates fall in.
In this example
Mon = false, tue = false, wed = false, thu=false, fri=true,sat=true,sun=true
I want to use this in a Vf page to render the somefields based on the boolean value.
Any pointers would be of great help.
Date has a method called toStartOfWeek which you could leverage, assuming your two dates do lie within the same week you could simply do something like this:
date weekStart = startdate.toStartOfWeek();
list<boolean> days = new list<boolean>();
for(integer i = 0; i < 7; i++)
{
days.add(weekStart.addDays(i) >= startdate && weekStart.addDays(i) <= enddate);
}
A little bit crude, but it'll give you an array of 7 boolean values. For longer/unknown ranges you could use a date cursor and increment that instead of an integer here, but this should get you started. Note, I've not tested this code ;)