Solved

Determine if its a new month

Posted on 2014-11-17
6
87 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 25

Accepted Solution

by:
Shaun Kline earned 250 total points
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:russell12
Comment Utility
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 32

Assisted Solution

by:it_saige
it_saige earned 250 total points
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now