Link to home
Start Free TrialLog in
Avatar of JimiJ13
JimiJ13Flag for Philippines

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 -User generated image-saige-
@it_saige:  Your PayCode extension method won't return correct value when input month is 12, I think.
@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 -User generated image-saige-
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 -User generated image-saige-
@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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JimiJ13

ASKER

Thank you Experts for the helps.