|
How to create new DateTime of months, going back 12 months
I'm writing some reports, that need dynamic months. Months going back a year, 12 Months from the current month.
So like today is Feb 17, 2020
Feb 1, 2020 to Feb 17, 2020
Jan 1, 2020 to Jan 31, 2020
Dec 1, 2019 to Dec 31, 2019
Nov 1, 2019 to Nov 30, 2019
etc
I tried this, M2 would be Jan 1, 2020, M3 would be Dec 1, 2019.
but it crashes on the M3, because the year rolls back to 2019. I get why it crashed.
Dim M2 = New DateTime(Now.Year, Now.Month -1, 1)
Dim M3 = New DateTime(Now.Year, Now.Month -2, 1)
Dim M4 = New DateTime(Now.Year, Now.Month -3, 1)
Dim M5 = New DateTime(Now.Year, Now.Month -4, 1)
Dim M6 = New DateTime(Now.Year, Now.Month -5, 1)
Dim M7 = New DateTime(Now.Year, Now.Month -6, 1)
Dim M8 = New DateTime(Now.Year, Now.Month -7, 1)
Dim M9 = New DateTime(Now.Year, Now.Month -8, 1)
Dim M10 = New DateTime(Now.Year, Now.Month -9, 1)
Dim M11 = New DateTime(Now.Year, Now.Month -10, 1)
Dim M12 = New DateTime(Now.Year, Now.Month -11, 1)
Then I'm curious on say Feb, if I create a stop date of say
Dim sM2 = New DateTime(Now.Year, Now.Month - 1, 31) since Feb has 28 or 29 days.
I suspect it will crash, haven't tried it yet.
Basically, these dates will be injected into a Linq group operation.
Well I guess I could write a function to calculate this, and pass months back and return a date with time of midnight.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Call the functions
Dim M9 = AmCommon.MakeMonthStartDate(-8)
Dim M10 = AmCommon.MakeMonthStartDate(-9)
Dim M11 = AmCommon.MakeMonthStartDate(-10)
Dim M12 = AmCommon.MakeMonthStartDate(-11)
Dim sM2 = AmCommon.MakeMonthStopDate(-1)
Dim sM3 = AmCommon.MakeMonthStopDate(-2)
Dim sM4 = AmCommon.MakeMonthStopDate(-3)
Dim sM5 = AmCommon.MakeMonthStopDate(-4)
Functions
Public Shared Function MakeMonthStartDate(byVal monthsBack as Integer) As DateTime
Dim month = DateTime.Now().AddMonths(monthsBack)
Dim fd = FirstDayOfMonth(month)
Return New DateTime(month.Year, month.Month, fd.Day, 0, 0, 0)
End Function
Public Shared Function MakeMonthStopDate(byVal monthsBack as Integer) As DateTime
Dim month = DateTime.Now().AddMonths(monthsBack)
Dim ld = LastDayOfMonth(month)
Return New DateTime(month.Year, month.Month, ld.Day, 23, 59, 59)
End Function
Public Shared Function FirstDayOfMonth(ByVal sourceDate As DateTime) As DateTime
Return New DateTime(sourceDate.Year, sourceDate.Month, 1)
End Function
Public Shared Function LastDayOfMonth(ByVal sourceDate As DateTime) As DateTime
Dim daysInMonth = DateTime.DaysInMonth(sourceDate.Year, sourceDate.Month)
Return New DateTime(sourceDate.Year, sourceDate.Month, daysInMonth)
End Function
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
|
That's funny Richard, so simple ..
I needed date blocks in months, probably a better way of expressing what I wanted.
Then I can take sales amount within the month blocks and put them in a report data set column
Sort of like ... This is a concept and not code...
FMTD_0 = SUM(FShipdate) >= Feb 1, 2020 And <= Feb 29, 2020
FMTD_1 = SUM(FShipdate) >= Jan 1, 2020 And <= Jan 31, 2020
FMTD_2 = SUM(FShipdate) >= Dec 1, 2019 And <= Dec 31, 2019
FMTD_3 = SUM(FShipdate) >= Nov 1, 2019 And <= Nov 30, 2019
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
So range 1 would be M1 to Today ; range 2 would be M2 to M1.AddDays(-1) ; etc.
Although it might be simpler to tweak the end condition slightly:
- >= 2020/02/01 And < 2020/03/01
- >= 2020/01/01 And < 2020/02/01
- >= 2019/12/01 And < 2020/01/01
- etc.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
HOW TO CREATE RDLC REPORT AND CALL IN VS 2013
|
|
|
|
|
|
Hi experts
Pls someone help me. I need retrieve data from listview to textbox, comboBox, Option Button and etc. Thanks a lot
|
|
|
|
|
What exactly is the problem?
|
|
|
|
|
Problem is. I need retrieve data in listview when I selected item in listview then transfer the data in textbox.
Example
I click the lastname in listview the show the clicked item in textbox
|
|
|
|
|
Use the click event and identify the selected item in the list box
get the text property of the selected item into a variable
update the text property of the textbox with the text from the variable.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
DataGrid with different controls
to excel with cell color in vb.net
I have already tried this code below like this:
Export ASP.NET DataGrid to Excel[^]
But I got some errors below like following these
1.Conversion from type 'ListItem' to type 'String' is not valid.
2.The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
|
|
|
|
|
Maybe with some code we can see whats wrong.
anyway: listitem and string are not the same type. listitem.text may help.
Jan
|
|
|
|
|
I have a weird situation where (randomly) a previous Cell value is read by the Excel VBA code.
eg: Cell 1A had a value = Y. It was updated to N. However a subsequent VBA code read the cell value as Y
It has been hard to troubleshoot as this was a random occurrence. I was lucky to catch one today, and upon stepping through the code (using F8) discovered that the correct current value was read during debug!. Re-run the macro without debug mode and the correct value was read.
Its very confusing and I cannot think of anything but an Excel bug.
Have you encountered this issue at all and if so what solution/s have you applied.
Thanks in advance.
|
|
|
|
|
With the amount of people that have been using Excel, it is very unlikely that there's a bug that's that basic. Also, the computer doesn't do stuff "randomly"; that's simply our way of describing stuff when we can't see the pattern.
Is there timing involved in your code? Can you reproduce the error?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Male sure you are reading the same value IE CurrentValue, excel has the ability to roll back changes and you may be reading from the wrong/different property.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
No, but you should probably post the relevant code.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
May I have some help on coding this in VB.net console app
If the user selects the ‘Enter RLE’ option:
a. the user is asked how many lines of RLE compressed data they want to enter
b. the program should check that the number entered is greater than 2 and if it isn’t display a suitable error message and get the user to keep re-entering the number until it is valid
c. if the user entered a valid number, they then enter the compressed data one line at a time until they have entered the specified number of lines
d. once all the compressed data has been entered, the program decompresses the data and displays the ASCII art image
e. the user is returned to the main menu.
|
|
|
|
|
Which of these points are you having troubles with? We won't do all the work for you.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
phil.o wrote: We won't do all the work for you. Booo! You're no fun.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
The forum is for asking questions and getting solutions - NOT ABUSE!
|
|
|
|
|
Member 14721179 wrote: NOT ABUSE! We are not abusing you. All you have said is "somebody do all my work for me." You are the one trying to abuse us.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
ZurdoDev wrote: Booo! You're no fun. As weird as it can sound, I would like to be able to be fun at any time. I must confess I often fail, though.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
asked for few solutions. Didn't ask you to complete the whole code. 
|
|
|
|
|
You didn't ask anything at all. You just posted a bunch of "requirements" for an application and left it at that.
You never defined what you meant by "help".
There is nothing in this app that is complicated at all. It's just an input, an if statement, loops, and printing. Every piece of this app would have been covered in your class.
|
|
|
|