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?
 
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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.