find the Day of week of a particular date - salesforce

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 ;)

Related

Querying from SQL Server datetime conditions

I have a datetime stored in SQL Server like '2021-10-12 18:46:31.047' and '2021-10-225 23:54:08.667' in one row..
How can I query from datetime by using date only not hours
WHERE str_day = '2021-10-23' AND end_day = '2021-10-24'
Help please
Use date boundaries:
WHERE str_day >= '20211023'
AND str_day < '20211024'
AND end_day >= '20211024'
AND end_day < '20211025'
If your columns str_day and end_day are of time DATETIME (or DATETIME2(n)), then you can use something like this:
WHERE CAST(str_day AS DATE) = '2021-10-23'
AND CAST(end_day AS DATE) = '2021-10-24'
DATE is the date-only (no time portion) datatype - if your time portion is never needed, it might be useful to store those things as DATE from the beginning...
Use this; this will work for many scenario and types
WHERE str_day = '23 OCT 2021'
AND end_day = '24 OCT 2021'

Measure does not work for Month Threshold

I build this Dax measure
_Access_Daily = CALCULATE(
DISTINCTCOUNTNOBLANK(ApplicationAccessLog[ApplicationUserID]),
FILTER('Date','Date'[DateId]=SELECTEDVALUE('DateSelector'[DateId],MAX('DateSelector'[DateId]))))+0
_Access__PreviousDay = CALCULATE(
DISTINCTCOUNTNOBLANK(ApplicationAccessLog[ApplicationUserID]), FILTER('Date','Date'[DateId]=SELECTEDVALUE('DateSelector'[DateId],MAX('DateSelector'[DateId]))-1 ))+0
The Date Selector table is a disconnected table containing dates from the 20th Jan to now. Dateid is a whole number like 20200131.
The Date table is a standard date table with all the dates between 1970 and 2038. Date id is a whole number like 20200131.
However it does not seems to work for the month threshold between Jan and Feb ? So if selected date is 01/02/2020 then it does not return correctly for the 31/01/2020.
As mentioned in the comments, the root problem here is that the whole numbers you use are not dates. As a result, when you subtract 1 and cross month (or year) boundaries, there is no calendar intelligence that can adjust the numbers properly.
Your solution (using 'Date'[DayDateNext]) might work, and if for some additional considerations this design is a must, go with it. However, I'd suggest to revisit the overall approach and use real dates instead of "DateId". You will then be able to use built-in DAX time intelligence, and your code will be more elegant and faster.
For example, if your "Date" and "DateSelector" tables have regular date fields, your code can be re-written as follows:
_Access_Daily =
VAR Selected_Date = SELECTEDVALUE ( 'DateSelector'[Date], MAX ( 'DateSelector'[Date] ) )
VAR Result =
CALCULATE (
DISTINCTCOUNTNOBLANK ( ApplicationAccessLog[ApplicationUserID] ),
'Date'[Date] = Selected_Date
)
RETURN
Result + 0
and:
_Access_PreviousDay =
CALCULATE ( [_Access_Daily], PREVIOUSDAY ( 'Date'[Date] ) )

How to find out which of the games happened on Mondays only?

I have tried 2 codes, the first one hasn't worked, while the second has. I basically have to display how many games were played on Mondays and show the teams that played them.
MATCH (t:Teams)
WHERE date({year:2019, month: 1 }) > t.Date <= date({year:2018, month:12})
RETURN t.HomeTeam AS HomeTeam,
t.AwayTeam AS AwayTeam,
t.Date AS Date
The result is: (No changes, no records) - nothing
MATCH (t:Teams)
WITH [item in split(t.Date, "/") | toInteger(item)] AS dateComponents
WITH ({day: dateComponents[0], month: dateComponents[1], year: dateComponents[2]}).dayOfWeek AS date
WHERE date = 1
RETURN COUNT(*)
The result is: Count(*) 0
I think there may be a couple of things going on in your first query. The date matching line
WHERE date({year:2019, month: 1 }) > t.Date <= date({year:2018, month:12})
is looking for a date that is less than 20190101 and less than or equal to 20181201. If you are actually looking for a date between those two values you need to change the operator to greather than equals for 201801.
That said, if Date is actually a string then the date comparison will not work either.
In your second query, it looks like you decided that Date was indeed a string and you split it up but still did not get any results. Although you break the date string up into its components you did not supply the date() function around your date components in this line...
WITH ({day: dateComponents[0], month: dateComponents[1], year: dateComponents[2]}).dayOfWeek AS date
Try this for your second query.
MATCH (t:Teams)
WITH [item in split(t.Date, "/") | toInteger(item)] AS dateComponents
WITH date({day: dateComponents[0], month: dateComponents[1], year: dateComponents[2]}).dayOfWeek AS date
WHERE date = 1
RETURN COUNT(*)

salesforce SOQL can't get datetime in local time

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.

Converting day, weeknr and year to date

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);

Resources