Mid-Month and Month-End based Code

We do Payroll preparation twice a month, 1st on the 15th with PayCode as MM15yy and the Month End with variable PayCode as follows:
MM28yy,MM29yy, MM30yy or MM31yy.

This is what we like to happen:
If the payroll is generated from 6th to 20th of the month, PayCode will be auto generated as MM15yy and the rest shall be
Month End PayCode (MM28yy,MM29yy, MM30yy or MM31yy.)  
 
Any help would be greatly appreciated.
JimiJ13I T ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
A sample function to generate PayCode based on current date (DateTime.Now):

function GeneratePayCode()
{
    DateTime  today = DateTime.Now;

    if (today.Day <= 5) // generate PayCode for previous month
    {
        if (today.Month == 1) // generate PayCode for End-Month of December, last year
            return new DateTime(today.Year - 1, 12, 31).ToString("MMddyy");
        else
            return new DateTime(today.Year, today.Month - 1, DateTime.DaysInMonth(today.Year, today.Month)).ToString("MMddyy"); // generate PayCode for End-Month of previous Month, same year
    }
    else
    {
         if (today.Month <= 20) // generate PayCode for Mid-Month of this Month
             return new DateTime(today.Year, today.Month, 15).ToString("MMddyy");
         else
             return new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month)).ToString("MMddyy"); // generate PayCode for End-Month of this Month

    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
The following code:
using System;

namespace EE_Q28641229
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Paycode for 12/04/2015 = {0}", "12/4/2015".GetPayCode());
			Console.WriteLine("Paycode for 06/20/2015 = {0}", new DateTime(2015, 6, 20).GetPayCode());
			Console.WriteLine("Paycode for {0} = {1}", DateTime.Now.ToString("MM/dd/yyyy"), DateTime.Now.GetPayCode());
			Console.WriteLine("Paycode for 01/01/2016 = {0}", new DateTime(2016, 1, 1).GetPayCode());
			Console.WriteLine("Paycode for bad string = {0}", "bad string".GetPayCode());
			Console.ReadLine();
		}
	}

	public static class Extensions
	{
		public static string GetPayCode(this string date)
		{
			var result = DateTime.MinValue;
			if (!DateTime.TryParse(date, out result))
				return "Invalid date time string entered!!!";
			return GetPayCode(result);
		}

		public static string GetPayCode(this DateTime date)
		{
			if (date.Day <= 5)
				return new DateTime(date.Year, date.Month, 1).AddDays(-1).ToString("MMddyy");
			else if (date.Day >= 21)
				return new DateTime(date.Year, date.AddMonths(1).Month, 1).AddDays(-1).ToString("MMddyy");
			else
				return new DateTime(date.Year, date.Month, 15).ToString("MMddyy");
		}
	}
}

Open in new window

Produces the following results -Capture.JPG-saige-
0
Duy PhamFreelance IT ConsultantCommented:
@it_saige:  Your PayCode extension method won't return correct value when input month is 12, I think.
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

it_saigeDeveloperCommented:
@Duy Pham - Sure it will.

Proof of concept -
using System;
using System.Linq;

namespace EE_Q28641229
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("@Duy Pham - Sure it will");
			foreach (var date in (from d in Enumerable.Range(1, DateTime.DaysInMonth(2015, 12)) select new DateTime(2015, 12, d)))
				Console.WriteLine("Paycode for {0} = {1}", date.ToString("MM/dd/yyyy"), date.GetPayCode());
			Console.ReadLine();
		}
	}

	public static class Extensions
	{
		public static string GetPayCode(this string date)
		{
			var result = DateTime.MinValue;
			if (!DateTime.TryParse(date, out result))
				return "Invalid date time string entered!!!";
			return GetPayCode(result);
		}

		public static string GetPayCode(this DateTime date)
		{
			if (date.Day <= 5)
				return new DateTime(date.Year, date.Month, 1).AddDays(-1).ToString("MMddyy");
			else if (date.Day >= 21)
				return new DateTime(date.Year, date.AddMonths(1).Month, 1).AddDays(-1).ToString("MMddyy");
			else
				return new DateTime(date.Year, date.Month, 15).ToString("MMddyy");
		}
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-
0
it_saigeDeveloperCommented:
And here is for January in case you are worried about previous year.  Proof of concecpt:
using System;
using System.Linq;

namespace EE_Q28641229
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("@Duy Pham - Sure it will");
			foreach (var date in (from d in Enumerable.Range(1, DateTime.DaysInMonth(2015, 1)) select new DateTime(2015, 1, d)))
				Console.WriteLine("Paycode for {0} = {1}", date.ToString("MM/dd/yyyy"), date.GetPayCode());
			Console.ReadLine();
		}
	}

	public static class Extensions
	{
		public static string GetPayCode(this string date)
		{
			var result = DateTime.MinValue;
			if (!DateTime.TryParse(date, out result))
				return "Invalid date time string entered!!!";
			return GetPayCode(result);
		}

		public static string GetPayCode(this DateTime date)
		{
			if (date.Day <= 5)
				return new DateTime(date.Year, date.Month, 1).AddDays(-1).ToString("MMddyy");
			else if (date.Day >= 21)
				return new DateTime(date.Year, date.AddMonths(1).Month, 1).AddDays(-1).ToString("MMddyy");
			else
				return new DateTime(date.Year, date.Month, 15).ToString("MMddyy");
		}
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-
0
Duy PhamFreelance IT ConsultantCommented:
@it_saige:  You can easily see in your POC PayCode result of December, from 12/21/2015 --> 12/31/2015, the result is '123114' while it should be '123115'. Or am I wrong here?
0
it_saigeDeveloperCommented:
@Duy Pham...  I get what you mean now.  Thanks for pointing that out...  Changing the position of AddMonths() resolves the issue:
public static class Extensions
{
	public static string GetPayCode(this string date)
	{
		var result = DateTime.MinValue;
		if (!DateTime.TryParse(date, out result))
			return "Invalid date time string entered!!!";
		return GetPayCode(result);
	}

	public static string GetPayCode(this DateTime date)
	{
		if (date.Day <= 5)
			return new DateTime(date.Year, date.Month, 1).AddDays(-1).ToString("MMddyy");
		else if (date.Day >= 21)
			return new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1).ToString("MMddyy");
		else
			return new DateTime(date.Year, date.Month, 15).ToString("MMddyy");
	}
}

Open in new window

December output -Capture.JPGJanuary output -Capture.JPG-saige-
0
JimiJ13I T ConsultantAuthor Commented:
Thank you Experts for the helps.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.