Get the month
Moderators: chulett, rschirm, roy
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
Get the month
Hi,
I have the following function I use which gets me yesterday's date in format YYYYMMDD:
Oconv(Date() - 1), "DYMD[4,2,2]" : @VM : "MCN")
I would like to use something similar but, I would like it to return the previous month from today's Date. To be in the same format YYYYMMDD.
Example, today is 20160122
Result would be: 20151201 (yes would like to default it to the first day of the previous month)
Another example
Current date is : 20160318
Result: 20160201
Thanks for any help ahead of time.
I have the following function I use which gets me yesterday's date in format YYYYMMDD:
Oconv(Date() - 1), "DYMD[4,2,2]" : @VM : "MCN")
I would like to use something similar but, I would like it to return the previous month from today's Date. To be in the same format YYYYMMDD.
Example, today is 20160122
Result would be: 20151201 (yes would like to default it to the first day of the previous month)
Another example
Current date is : 20160318
Result: 20160201
Thanks for any help ahead of time.
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
Code: Select all
OCONV(ICONV(OCONV(DATE(),"DY/M[4,2]"):"/01","D/YMD[4,2,2]")-1,"D/YM[4,2]"):"/01"
Then convert it back to internal format and subtract a day to get the last day of the previous month.
Convert this to a YYYYMM string and add "01" to get the first day of the previous month.
Last edited by ArndW on Fri Jan 22, 2016 12:02 pm, edited 1 time in total.
<a href=http://www.worldcommunitygrid.org/team/ ... TZ9H4CGVP1 target="WCGWin">
</a>
</a>
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
Would there not be an easier way using MONTH.FIRST. I only bring this up from other threads I'm reading.
Also now that I'm thinking about it I would need to be able to specific how many months to go back. I may need to reload anywhere from 1 month to 12 months ago. I can use a loop counter variable for this if there is the ability to subtract a month using the counter.
Also now that I'm thinking about it I would need to be able to specific how many months to go back. I may need to reload anywhere from 1 month to 12 months ago. I can use a loop counter variable for this if there is the ability to subtract a month using the counter.
(Note, I edited my BASIC code from the original post due to an error, the code now in the thread works)
I hadn't thought about MONTH.FIRST, but it wouldn't completely remove ICONV/OCONV here, it could give you the first day of the current month, but then you'd still have subtract a day and convert the format.
I hadn't thought about MONTH.FIRST, but it wouldn't completely remove ICONV/OCONV here, it could give you the first day of the current month, but then you'd still have subtract a day and convert the format.
<a href=http://www.worldcommunitygrid.org/team/ ... TZ9H4CGVP1 target="WCGWin">
</a>
</a>
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
Not sure why you keep mentioning a 'loop counter' as there's no need to loop to go back more than one month. So you just need an offset to drive how many months to go back? I'd suggest you go to Ray's site (coming soon!) and click on any of his download links. Cancel that download (or add it to your Bag O' Tricks) and then download his Date Routines dsx export file. Importing it will put a plethora of "date math" routines into your project, including one to AddMonths which sounds like one you could easily leverage here.
-craig
"You can never have too many knives" -- Logan Nine Fingers
"You can never have too many knives" -- Logan Nine Fingers
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm
-
- Premium Member
- Posts: 48
- Joined: Wed Jan 07, 2015 3:30 pm