JimiJ13
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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 -
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");
}
}
}
Produces the following output --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");
}
}
}
Produces the following output --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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Experts for the helps.
Open in new window
Produces the following results --saige-