Solved

Determine if its a new month

Posted on 2014-11-17
6
94 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 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

635 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