How can I determine if the current date is within/outside a range of two dates that don't include a year? - objective-c

So I have an object (Activity) where I allow users to select start date and an expiry date for an activity.
The user is basically a center/centre that provides activities for the public.
They have the option of making these activities seasonal. So for example an activity could run from Feb 01 to June 22. I use the current date along with a bunch of code to determine if the activity is in season or not.
So for example I check if the current date 271014 is greater than the start date 020114 and less than the expiry date 062214. If it is then it means the date is in range.
The issue:
I've had to make changes, so now the user must select a month and day only. This is fine until they select something like Nov 01 to Feb 28.
These dates would be in the format 1101 and 2802.
Without the year this obviously gives me different results. With the year I would have been able to determine that the expiry date Feb 28 was the year after the start date and not the same year. Then when working out if the current date was within the start date and expiry date I'd get expected results. Without the year the expiry date is now actually not greater than the start date even though it should be.
What I'm currently doing
How do I solve this issue? All I really need is to allow a user to set a start date and an expiry. The activity will show up in a table view only when the current date is in range.
If only an expiry date is selected then the activity will remain active until that expiry date has been reached. Even if I set and expiry date for Feb 28 and we're in November. The same need applies to the start date too.
Would appreciate some insight here.
Thanks for your time.

Here my suggestion (in pseudo-code): Convert all dates to strings in the format "MMdd", in
your example
startDate = "1101" // November 1
expiryDate = "0228" // February 28
currentDate = "1027" // October 27
If startDate <= expiryDate then the range is within the same year and you can check
the current date with
if (startDate <= currentDate && currentDate <= expiryDate) ...
Otherwise the range starts in one year and ends in the next year, and you check with
if (currentDate >= startDate || currentDate <= expiryDate) ...

Related

Specific Month-day select statement query

I would like to select from a table where the date falls within a specific time each year f.e:
select * from Customer where date >= August 15th and date <= December 20th
Since this will be for a report that runs every year, I do not want to hardcore the date as I will have to change it every year. I would like to have it dynamic to pick the date from August 15th to December 20th of the current year.
I have the below query where I can retrieve the Month and Date:
SELECT DATENAME(month, date) AS Month,DATENAME(day, date) AS Day from Customer
However, I am struggling to have this selection date range.
TIA
In SQL SERVER 2017
maybe this can help you:
SELECT * FROM Customer WHERE date BETWEEN
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'08','15')) AND
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'12','20'))
This add the current year to a concatenated date you want, then convert it all into datetime type..

Snowflake retrieving data of past month on a particular date of current month

I am new to snowflake and my manager wants me to retrieve the data of the past month when it is 5th of the current month. For example if today is 5th April, then ask snowflake to retrieve the data of the past month i.e. from 1st March 2021 to 31st March 2021 and similar for all the other months.
The reason why he wants to update the last month data on 5th of every next month because that is the day when we get the data.
I tried to use the DATEADD function but it is more complicated than just using this function.
Thanks in advance!
PS: The data for every month has same date. for example: the date is like - April 20th will be stored in the database as "2021-4-01" - and same for April 25th date will be stored as "2021-4-01" .
The day doesn't change in the database, just the month and year.
as to the prior month window that can be done via DATE_TRUNC and DATEADD
select
current_date as cd
,date_trunc('month', cd) as end_range
,dateadd('month', -1, end_range) as start_range
;
gives:
CD END_RANGE START_RANGE
2021-04-21 2021-04-01 2021-03-01
the other half of the question only do it on the 5th, if you have a task run daily etc. can be solved via
,day(current_date) = 5 as is_the_fifth
or if in an inline way
iff(day(current_date) = 5, <do true stuff>, <do false stuff>)

Derive 445 Quarter for any given date

I need assistance writing a query that determines the quarter of a given date based on 445 FY calendar. The FY ends on first Friday of February every year.
For example in attached image the table has the order id and order created date. Based on the order created date I want to determine which 445 FY quarter it falls in.
Use a case statement with a between condition to divide up your custom quarters. You will need to enter the start and end dates of each quarter:
Select
Case
when date between 'startdateQ1' and 'enddateQ1' then 'Quarter1'
when date between 'startdateQ2' and 'enddateQ2' then 'Quarter2'
when date between 'startdateQ3' and 'enddateQ3' then 'Quarter3'
when date between 'startdateQ4' and 'enddateQ4' then 'Quarter4'
when date is null then 'Null'
else 'Unknown'
end
As Current_Quarter
From DB

Check if the current date string is within or outside the range of two other dates

I have 2 date pickers. One is for selecting a start date and the other is for selecting an expiry date. When a selection has been made I convert the date to string format and store the results in a two text fields.
This is how I save to the database:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d" // e.g. September 15
let sDate = dateFormatter.dateFromString(startDateField.text) as NSDate!
let eDate = dateFormatter.dateFromString(expiryDateField.text) as NSDate!
activity["startDate"] = sDate
activity["expiryDate"] = eDate
activity.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
What I intend to do is have a table view display 2 sections: one showing activities that are active, and the other showing activities that are expired/not in date range.
I figured I could do this by taking the current date and checking it was within the start date and expiry date range.
My saved date from above is showing up in parse like this:
The thing is, I don't want to take the year or time into account. The only thing that matters is the month and day. So for example an activity could run once a year between September 15th and December 20th.
I need a way to check that the current date is within those two dates taking the day (e.g. 15th) of the month into account. So today's date is October 19 and an activity would be in range if its start date was September 15 and its expiry date December 20, but would be out of range/operation if the expiry date was October 18 or start date was October 25.
So to summarise:
I will be displaying two sections in a table view. One with active activities and another with non-active out of operation activities.
How do take my stored start date and expiry date and easily check the current date is within their range?
For the occasions when users pick either a start date or expiry date I need to check if the current date is greater/less than the start date (to be able to decide if the activity should show up or not) or if the current date is greater/less than the expiry date (to be able to decide if the activity has passed or not passed its expiry date.
It would be nice if I could roll 1 and 2 into one function and use it in the table view.
Would be interested to learn how the more experienced programmer would achieve this.
Instead of the date format "MMMM d" (e.g. "September 15"), you can convert the dates
to a string with the date format "MMdd" (e.g. "0915"). Then you can do a simple
string comparison between the current date, the start date and the expiry date.
For example,
"0915" < "1019" < "1220"
so October 19 is in the range from September 15 to December 20.

Get birthday greater than current date

my user table is as follows:
birhtMonth: int
dayOfBirthday: int
HireDate: Date
REQUIREMENT: i want to get all upcoming birthdays and hire dates (day/month year is excluded here) in next 6 months, putting in consideration that for current month the day should be greater than today, so here's what i did:
#Query("from User u where ( (u.birthMonth in (8,9,10,11,12,1)) or (month(u.hireDate) in (8,9,10,11,12,1)) ) and u.company = :company")
this gets all upcoming birthdays & hire dates in next six months but it gets birthdays & hire dates in this month for days before & after current day, and it should only get results > current day and ignore results < current day in this month.
EXAMPLE:
today's date is 8/3/2013, if there's birthday with birthMonth=8 and dayOfBirth=3 or 2 or 1 it should be ignored only dayOfBirth > 3 in current month is considered, also if there's hirDate like:
2011-08-01
2012-08-02
2012-08-03
they should be ignored too, please advise how to solve this in sql or hql.
Your hireDate is of type Date, so use a date comparison and use between such as:
(hireDate between :toDayParam and :sixMonthLaterParam)
for birthDate, you can compare with lpad(birthMonth, 2, 0) + lpad(birthDate, 2, 0) but you shall care about whether six month later is in next year or current year.
I think your looking for the DATE functions. They can greatly help you out here. Specifically the DATEADD function. Take a look at the code I made here.
SELECT * FROM dbo.product
WHERE dbo.product.stockDate > GETDATE()
AND dbo.product.stockDate < GETDATE() + DATEADD(month, 6, dbo.product.stockDate)
AND dbo.product.expirationDate > GETDATE()
AND dbo.product.expirationDate < GETDATE() + DATEADD(month, 6, dbo.product.expirationDate)
This will guarantee that the stockDate and the expirationDate are greater than the current date and less than the current date + 6 mo. DATEADD works as follows DATEADD(-what you want to increment by-, -how much you want to increment-, -date to add to-).