Page 1 of 1

Get the first and last day of previous month

Posted: Wed Jun 19, 2013 11:04 am
by raji33
Hi All,

I have requirement to get data between previous month first day and last day.

Below is the code i am using to get last day of previous month but not knowing how to get first day of last month. I have some info on Month.First(server edition) but i need in parallel . Any suggestions?

Code: Select all

DateFromDaysSince(-1, stringtodate(right(0:MonthFromDate(CurrentDate()),2):'-':'01':'-': YearFromDate(CurrentDate()),"%mm-%dd-%yyyy"))

Posted: Wed Jun 19, 2013 12:17 pm
by chulett
Getting the last day of a month is the tricky part, getting the first day should be simple since it is always '1' so hard-code it in your transformation much like you are already doing.

Posted: Wed Jun 19, 2013 2:43 pm
by raji33
Thanks chulett . I am not clear on hard-code '1' , let me give an example of what i am looking for
ex:
if i run job on june 1, 2013

output:
2013-05-01
2013-05-02
2013-05-03
-
-
-
- so on
2013-05-31

and if the month has 30 days it should display 30 days data.

Posted: Wed Jun 19, 2013 3:51 pm
by chulett
I understood what you are looking for, I do believe, at least in the original post. I was responding to your specific statement that you are "not knowing how to get first day of last month". You were already getting the "last month" according to what you posted so was just trying to point out that figuring out what the first day of that was is pretty darn straight-forward.

So... what exact help are you looking for here? It looks like you've just expanded the scope of your question, hence my question.

Posted: Wed Jun 19, 2013 5:53 pm
by ray.wurlod
What Craig means is that you can hard code day number as 1 to get the first of any month. However, you can use date functions exclusively.

For example: to get the first day of the current month, subtract the current day number from the date:

Code: Select all

DateFromDaysSince(1 - MonthDayFromDate(CurrentDate()), CurrentDate())
Assuming we have that result in stage variable svCurrentMonthFirst, to get the first day of the previous month you could use :

Code: Select all

DateOffsetByComponents(svCurrentMonthFirst, 0, -1, 0)
And to get the last day of the previous month you could use the fact that it is one day prior to the first day of the current month:

Code: Select all

DateOffsetByComponents(svCurrentMonthFirst, 0, 0, -1)

Posted: Wed Jun 19, 2013 11:57 pm
by jerome_rajan
ray.wurlod wrote:....

Code: Select all

DateOffsetByComponents(svCurrentMonthFirst, 0, -1, 0)
...
You've got to be really careful of using this. It won't always give you what you expect unless you set your expectations in line with how it really works :? .Just remembered this discussion viewtopic.php?t=149999

Posted: Thu Jun 20, 2013 12:16 am
by ray.wurlod
It WILL always give me what I expect, because I made sure that the date argument is the first of the month.
:)

Posted: Thu Jun 20, 2013 12:20 am
by jerome_rajan
I've been wary of this function ever since. Always try finding alternates, albeit longer and more cumbersome. There ought to be a more elaborate documentation replete with examples to explain the modus operandi of this function. :(

Posted: Thu Jun 20, 2013 12:26 am
by ray.wurlod
I think my research conclusions in the other thread suffice as documentation of the modus operandi, as you put it. This is How It Works, even though it's not what you want it to do.

Posted: Thu Jun 20, 2013 1:09 am
by bhasds
Hi Raji33,

You may try the below to get the first day of last month-

Code: Select all

CurrentDate()[1,4]:"-"If  CurrentDate()[6,2]-1 < 10 Then 0: CurrentDate()[6,2]-1 Else  CurrentDate()[6,2]-1):"-":"01

Posted: Thu Jun 20, 2013 3:50 am
by ray.wurlod
My method does not involve several data type conversions. Yours does. These all take machine cycles.

Also yours will fail if the current month is January.

Posted: Mon Jul 15, 2013 8:59 pm
by raji33
Thanks Ray it worked.