Solved

Determine if its a new month

Posted on 2014-11-17
6
91 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 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

756 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