?
Solved

Determine if its a new month

Posted on 2014-11-17
6
Medium Priority
?
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 1000 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 34

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 34

Assisted Solution

by:it_saige
it_saige earned 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

719 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