Solved

Determine if its a new month

Posted on 2014-11-17
6
90 Views
Last Modified: 2014-11-17
I have an application that I need to determine if it is a new month, and if it is, it will automatically reset a 4 digit number to 0000.
Example: It is 11/17/2014, the 4 digit number is 1234.  On 12/1/2014 I need to the 4 digit number to be 0000.

I have been trying to write the code for it and I can not come up with a solution.  This is what I have come up with do far:
            If Now.AddDays((Now.Day - 1) * -1).AddMonths(1).AddDays(-1) >= DateTime.Now Then
                casenumber = "0000"

            End If

Open in new window


This does not work.  Any help on this would be greatly appreciated!!
Thanks in advance!
0
Comment
Question by:russell12
  • 3
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 250 total points
ID: 40448474
Could it be something as simple as:
If Now.ToString("MM") <> Now.AddDays(-1).ToString("MM") Then

Or even:
If Now.Month <> Now.AddDays(-1).Month Then
0
 
LVL 2

Author Comment

by:russell12
ID: 40448495
Ok so we are so close!!  The only problem I am going to have with this is what if they do not use it on the 1st and they use it on the 2nd?  I think the only way around this is storing the current month that it was assigned and then comparing the 2.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40448510
Testing Shauns example:
Module Module1
	Sub Main()
		Console.WriteLine(String.Format("{0} and {1} are in the same month: {2}", Now, Now.AddDays(-1), CompareMonths(Now, Now.AddDays(-1))))
		Console.WriteLine(String.Format("{0} and {1} are in the same month: {2}", New DateTime(2014, 11, 30), New DateTime(2014, 12, 1), CompareMonths(New DateTime(2014, 11, 30), New DateTime(2014, 12, 1))))
		Console.ReadLine()
	End Sub

	Private Function CompareMonths(ByVal date1 As DateTime, ByVal date2 As DateTime) As Boolean
		Return date1.Month = date2.Month
	End Function
End Module

Open in new window


Produces the following output:Capture.JPG
There is no need to store the date because you are using Now and Now.AddDays(-1).

-saige-
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 2

Author Comment

by:russell12
ID: 40448588
His code works, the only issue is if a number is not generated on the 1st, but on the 3rd, the program will return false for a new month.
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 250 total points
ID: 40448636
True.  So then in that case, your response is correct, you would want to save the date for the last case created somehow, so that when a new case is created, the program will validate the months and rollover if a new month is detected.

-saige-
0
 
LVL 2

Author Closing Comment

by:russell12
ID: 40448733
I appreciate both of you commenting!!  I ended up saving the last case number date and I am doing a DateDiff statement!  Shaun Kline comment works great, the only reason I did not use it was the change from December to January.  Thanks again to both of you!  You both put me in the right path to correctly fix the issue!!  This is the piece of code I ended up using:
DateDiff(DateInterval.Month, lastmonthassigned, DateTime.Now) >= 1

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question