Checking if Date is between a range of dates - Logic Issue - BEGINNER - iphone

I have a Person object. and this person object has the following attributes;
Name
StartDate
EndDate
I am saving this Person objects to an array. This array might contain more than 100 person objects.All of the above attributes are Strings.
The following is an example of person objects in that array;
John, Tue Feb 22, Thr Mar 30
Jack, Wed Mar 09, Fri Apr 21
Jack, Thu Mar 19, Fri Dec 20
Jack, Tue Jan 08, Fri Apr 26 etc..
Now i need to will supply a date, say for example Wed 29 Mar, and i need to check if it's in the range of StartDate and EndDate in the array of persons object. I have a pseudo-code for the scenario.
The date i am going to check against the StartDate and EndDate are Strings too.
Pseudo-code:
if Startdate >= providedDate && EndDate <= providedDate {
// Add to an Array
else
//Do not add to an array
Since, the StartData, EndDate and ProvidedDate are all Strings how can i check if its Greater than or lesser than the provided date ?
Note: I need an approach that doesn't use NSPredicate

In order to compare values you need values that are comparable. You could use NSDate objects, you could cast dates into sortable character/numeric form (eg 20120425), or you could use custom objects that implement their own compare:options: methods.
Or you could write a separate compareString:string1 toString:string2 method and use the strings as you have them.
You just have to make up your mind which you want to do.

What are you being tested on here? It'd odd to have a date stored as a string. Are you meant to parse the string dates in order to compare them to the provided date? If so, I'll stay away from any kinds of date classes.
If so, I'd just split it into 3 strings -- day of the week, month, day. You don't care about day of the week.
You don't have a year so you can't do any logic about that... I guess just assume it's all in the same year. (You could do something crazy like calculate 'which recent year did this day fall on this day of the week', but come on...)
Associate each month with an integer. Let's say, using a hash map. As in Months.put('January',1), etc.
Get the number out of each date -- just convert from a string to an integer.
Then the logic for whether something is before or after a date is (if -1 is myMonth is before, 0 is they're the same, and 1 is myMonth is after)
if (Months.get(myMonth) < Months.get(providedMonth)) return -1
else if (Months.get(myMonth) > Months.get(providedMonth)) return 1
else
if (myDate < providedDate) return -1;
else if (myDate > providedDate) return 1;
else return 0;

Related

How to plot a graph for specific day of every month for whole Year MATLAB

I have two variables called x and y. Each has 24*365 values. 24 presents the number of hours in a day and 365 presents the number of days in a year. I am plotting the values of 12th hour and 25th day by the following command:
plot(x(12:12,25), y(12:12,25))
Now I want to do the same for every 25th day for a whole year. Like 25th of Jan, 25th of Feb, 25th of March. I am not bothered about values of hours but I don't know how to create its logic as every month has different number of days.
You can generate the day of year number by getting the datenum values for the 25th of each month and subtracting the datenum of the 1st Jan that year.
dayIdx = datenum(2022,1:12,25) - datenum(2022,1,1) + 1;
Then just use this as your column index
plot(x(12,dayIdx), y(12,dayIdx))
The choice of 2022 above is arbitrary, as long as you pick a non-leapyear to get the 365-day year correct.
The datetime data type is awesome for this type of work.
%Make a vector of datetimes
ts = ( datetime(2001,1,1,0,0,0):hours(1):datetime(2001,12,31,023,0,0) )';
%Find the datetimes which are on the 25th day of the month, and the 12th
%hour of the day
mask = (day(ts) == 25) & (hour(ts) == 12) ;
%Confirm
ts(mask)
The result is below.
(You likely want to use the mask variable itself for your task. Sometimes you want to use find on the logical statement to get a list of indexes instead.)
ans =
12×1 datetime array
25-Jan-2001 12:00:00
25-Feb-2001 12:00:00
25-Mar-2001 12:00:00
25-Apr-2001 12:00:00
25-May-2001 12:00:00
25-Jun-2001 12:00:00
25-Jul-2001 12:00:00
25-Aug-2001 12:00:00
25-Sep-2001 12:00:00
25-Oct-2001 12:00:00
25-Nov-2001 12:00:00
25-Dec-2001 12:00:00

Wrong day when using day()-formula with format - PowerBI

I'm trying to find out the weekday i.e Mon, Tue, Wed etc. from a date-range formatted as yyyy mm dd
I tried to use the formula format(day(Date Table),"ddd"), but the weekday is wrong. In my example, the output of 2020.01.01 gives Sunday, but it should be Wednesday.
I think your formula is wrong:
Instead of
format(day(Date Table),"ddd")
Use
format(<Target Table>[<date column>],"ddd")
I.e. Omit the DAX DAY call. This is resulting in the day of the month (1..31) being passed to the format function.
When you use the DAY function in DAX, it returns the day of the month (1 through 31).
Thus DAY ( DATE ( 2020, 1, 1) ) = 1 which means you're trying to format the number 1 as a date. Integers are interpreted as days since 1899/12/30 when treated as a date, so 1 corresponds to 1899/12/31, which happened to be a Sunday. Thus FORMAT(1, "ddd") = "Sun".
There's no reason to get DAY involved here. You can simply write
Day = FORMAT ( 'Calendar'[Date], "ddd" )

Dateparse MON in Tableau

I have a data source that returns a date as a string in the form of 'MON YYYY' (APR 2014, MAY 2014, etc.).
I tried making a calculated field off of this information with the following formula:
DATEPARSE('MMM YYYY', [Field1])
This is a sample set of the data I'm getting (I added the pipe as a divider):
Field1 || Calculated Field
APR 2014 || 12/22/2013
APR 2015 || 12/28/2014
APR 2016 || 12/27/2015
AUG 2014 || 12/22/2013
AUG 2015 || 12/28/2014
AUG 2016 || 12/27/2015
I've also tried to add a day field, but that results in the same incorrect data as above:
DATE(DATEPARSE('dd MMM YYYY','01 ' +[Field1]))
Is there something I'm perhaps misunderstanding about the dateparse function?
It turns out that YYYY means something totally different than yyyy. The capitalized MMMwas necessary for the MON type description. This worked for me:
DATE(DATEPARSE('MMM yyyy',[Field1]))
If you date the date off you'll get the hour, minute, second fields as well.
Dateparse converted it from a string [Field1] into a Date type using the aforementioned format of three digit month, a space, and a four digit year (e.g. AUG 2014 -> 8/2/2014).

Combine, Group and Rename

Here is my current formula:
if left ({Command.NextDueDate},7) = "2016-01" then "January'16"
else if left ({Command.NextDueDate},7) = "2016-02" then "February'16"
...
else if left ({Command.NextDueDate},7) = "2017-01" then "January'17"
I take date Strings from my database formatted like 2016-01-05, 2016-01-06...2017-01-01. I use these as columns.
I want to look for the year (16), then month (01), then for next column look for year (16) again and month (2). Then I'd like to interpret the month as shortened month names, like Jan or Feb. Finally I'd want to join the data back together. So in the end it will work like this:
16 + 01 becomes Jan16
16 + 02 becomes Feb16
17 + 01 becomes Jan17
How can I do this without manually entering an if-else clause for each month of each year?
No need to hard code it - Use this formula to get the month and year:
MonthName(Month(CDateTime({Command.NextDueDate}))) +
Right(Left({Command.NextDueDate},4),2);
Don't use if/else statements when you can get the information through clever use of formulas. There are a few ways you can make Ankur's formula even better:
To get the short Month name, use: MonthName(Month(CDateTime({Command.NextDueDate})),True) The True at the end shortens the name to "Jan", "Feb", "Mar" and so on.
To get the Year, use Year(CDateTime({Command.NextDueDate}))
Combine the two formulas and it does all the work for you:
MonthName(Month(CDateTime({Command.NextDueDate})),True) +
Year(CDateTime({Command.NextDueDate}))

How do I change constraints in a dojo DateTextBox dynamically?

I tried to do this:
dojo.mixin(endDate.constraints, {min: new Date(2009,09,14)});
But as a result I got this:
min Wed Oct 14 2009 00:00:00 GMT+0200 (CET)
??? It always adds one month! Is this a bug?
But what I actually want to do is something like this:
dojo.mixin(endDate.constraints, {min: dijit.byId("beginDate").date});
This results in:
min undefined
It's not a bug - it's a feature! And it's not a feature of Dojo, but JavaScript:
Integer value representing the month,
beginning with 0 for January to 11 for
December.
In order to debug that error, just use FireBug to see 1) what dijit.byId("beginDate").date returns - a string or a date object?, 2) if it's a string, is it correctly formatted; can new Date parse it?, etc...
Ben, as for the second part of your question, there is no date property on a DateTextBox. What you want is the value attribute
dijit.byId("beginDate").attr("value")
which does return a Date object.